string mixin only works once per block?

John Colvin john.loughran.colvin at gmail.com
Fri Apr 11 07:57:14 PDT 2014


On Friday, 11 April 2014 at 14:05:17 UTC, Vlad Levenfeld wrote:
> I'm trying to cut down on some boilerplate with this function:
>
> const string link (alias variable) ()
> {
> 	const string name = __traits (identifier, variable);
> 	return name~`= glGetUniformLocation (program, "`~name~`");`;
> }
>
> Applied in this kind of context:
> 		
> this ()
> {
> 	super ("default.vert", "gradient.frag");
> 	mixin link!start_color;
> 	mixin link!final_color;
> 	mixin link!center_pos;
> 	mixin link!lerp_vec;
> 	mixin link!lerp_range;
> }
> 					
>
> And the first mixin in every such function works fine, but the 
> rest don't. Specifically, I get this error:
>
> source/main.d(483): Error: mixin link!final_color link isn't a 
> template
>
> for each mixin after the first. If I put the argument to mixin 
> in parenthesis:
>
> mixin (link!start_color);
>
> I get:
>
> source/main.d(483): Error: value of 'this' is not known at 
> compile time
>
> Finally, removing the parenthesis and putting each mixin 
> statement in its own block:
>
> {mixin link!start_color;}
>
> compiles, but the mixed-in string has no apparent effect.
>
> Is this a bug or am I doing it wrong?

I'm not sure exactly what's going on here, but here's a few bits 
of info you might find useful.

mixin mixinTemplate!Params;

is for template mixins http://dlang.org/template-mixin.html

mixin(someString);

is for string mixins http://dlang.org/mixin.html


template mixins can only insert declarations/definitions, string 
mixins can inject arbitrary code i.e. anything.


When working with string mixins, pragma(msg allows you to print 
the result at compile-time to check what code is being generated. 
http://dlang.org/pragma.html


More information about the Digitalmars-d-learn mailing list