Mixin Expressions, can't evalutate string variable

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Aug 5 12:26:56 PDT 2010


Hmm.. ok. Adding immutable helps. I'm still a bit confused tho,
 because this will not compile:

string input2 = "int y;";
mixin(input2);

But this will compile:

immutable string input2 = "int y;";
mixin(input2);

And this too will compile:

string returnString(string input)
{
    return input;
}

mixin(returnString("int y;"));

Tomek Sowiński Wrote:

> Andrej Mitrovic napisał:
> 
> > I think this should work:
> > 
> > string s = "int x;";
> > mixin(s);
> > 
> > void main()
> > {
> >     
> > }
> > 
> > But I get:
> > testtest.d(11): Error: argument to mixin must be a string, not (s)
> > 
> > I get a similar error with the template example from the docs:
> > 
> > template GenStruct(string Name, string M1)
> > {
> >     string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
> > }
> > 
> > mixin(GenStruct!("Foo", "bar"));
> > 
> > void main() { }
> > 
> > testtest.d(27): Error: argument to mixin must be a string, not (GenStruct)
> > 
> > 
> > This is really weird, I swear it worked for me a couple of days ago. :s
> > Maybe I accidentally modified a library file, so I'll have a look.
> > Meanwhile can someone succesfully compile these?
> 
> template GenStruct(string Name, string M1)
> {
>     immutable string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
> }
> 
> It must be immutable to be seen as a template expression. Otherwise it's one mutable string declaration that happens to be named 
> same as the enclosing template.
> 
> 
> Tomek



More information about the Digitalmars-d mailing list