stringof and mixins when the types aren't available

Adam D. Ruppe destructionator at gmail.com
Sun Jun 16 17:10:56 PDT 2013


Consider the following:

module a;
T getInit(T)() {
    return mixin(T.stringof ~ ".init");
}

module b;

struct S {}
void main() {
    import a;
    auto s = getInit!S();
}


If we compile, we'll get this:

testmixin1.d(3): Error: undefined identifier S, did you mean 
module a?
testmixin2.d(6): Error: template instance a.getInit!(S) error 
instantiating


Now, in this case, the solution is obvious, ditch the mixin. But 
what about more complex cases, like parsing stringof to get 
default arguments or generating a whole class in a string mixin?



One technique I've used is instead of generating the whole class, 
just generate the innards and wrap it in a mixin template. Then, 
in the other module, write:

class Foo {
     mixin MakeFoo!();
}

and things will work since the mixin template will use this scope.


But I haven't solved the default arguments out of .stringof yet, 
which is similar to the dummy code at the top of this post. If 
you'd like to see my current code, search my web.d for 
parameterDefaultOf.

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/web.d

That code is so hideous btw, I think if I was redoing it today 
from scratch it would be a lot simpler, this was my first attempt 
at doing this kind of reflection thing. But somehow, it works.

Anyway, I get the default by parsing stringof, and when it is 
time to use it, I mix it in to get the value. This works for a 
lot of things, strings, ints, etc., built in types basically. But 
it fails for enums or structs (except those defined in web.d 
itself) because they aren't in that scope.

The type is semi-known though, it assigns it via a 
ParameterTypeTuple, but the actual name isn't here.


Any ideas to solve this, or do you have any techniques for string 
mixins you'd like to share for general knowledge?


More information about the Digitalmars-d mailing list