String mixin in templates

Andre andre at s-e-a-p.de
Tue Sep 18 08:28:06 PDT 2012


Hi,

I have the coding below. The statement mixin(test1("var")); does
exactly what I want. I wonder whether there is maybe a shortcut.
Instead of writing mixin(test1("var")) I want to write s.th. like
test2!("var"). Inside the template the string mixin should happen.
But the coding does not compile, due to the fact that the
variable var is not known to the template.
Is there any other nice shortcut, which has the needed scope?

For my scenario it is necessary that the variable name is handed
over as string and the generated code has access to this scope.

Kind regards
Andre

module main;
import std.stdio;

string test1(string str){
	return "writeln(" ~ str ~ ");";
}

template test2(string str){
	void test2(){
		mixin("writeln(" ~ str ~ ");"); 		
	}
}

void main(){
	string var = "Hello World!";
	
	// Working
	mixin(test1("var"));
	
	// Not working - template does not know var
	test2!("var");
}


More information about the Digitalmars-d-learn mailing list