mixins and compile-time coding take 2

janderson askme at me.com
Wed Feb 7 08:50:32 PST 2007


Although not my favorite idea (when compared to my static idea) here's 
another crazy compile-time idea.

What about a subset of compile time code (kinda like a scripting 
language, DMDScript could be used I guess, although D is just fine for 
me)? Execute would compile and run the code given.  You would also be 
able to pass in parameters into execute which:

Maybe the syntax needs a little work.

mixin(execute(T ...)(" return T[0]", "int foo = 5;"));  //Results in 
"int foo = 5;"

or this way so you can alias the execute:

mixin(execute(T ...)(" return T[0]")( "int foo = 5;") ~
	execute(T ...)(" return T[0]")( "int foo2 = 15;")
);

would translate to:

alias execute(T ...)(" return T[0]") fooExecute;

mixin(fooExecute("int foo = 5;") ~ fooExecute("int foo2 = 15;"));

An extension to this syntax since it really is a form of template:

template fooExecute(T...)
{
	"return T[0]"
}

Maybe values could be passed by name so that you could for instance 
write some something like:

template fooExecute(T...)
{
   //ie T[0] could be "return" and T[1] could be "int foo = 5"
	"T[0] \"T[1]\""
}

//Ok I guess that's only one change away from my static suggestion
template execute(T...)
{
	char [] foo(T t) //Detect at compile time that this uses the minimum 
set of allowable commands
	{
		return T[0];
	}
}


-Joel



More information about the Digitalmars-d mailing list