mixin string evaluation

Ali Çehreli acehreli at yahoo.com
Sun Jul 21 10:15:41 PDT 2013


On 07/21/2013 09:26 AM, Martin Krejcirik wrote:
> Is it somehow possible to evaluate a string on template parameters ?
> Something like this:
>
> enum source = "TYPE var;";
>
> template Eval(TYPE)
> {
> 	string Eval()
> 	{
> 		return evaluate(source);  <-- replace TYPE with int
> 	}
> }
>
> mixin(Eval!int); <-- int var here
>
>
> Basically I need a string mixin inserted into the template before the
> template parameters are evaluated.
>

import std.algorithm;

enum source = "TYPE var;";

template Eval(TYPE)
{
     string Eval()
     {
         string statement = source;
         auto found = statement.findSkip("TYPE");
         assert(found);

         return TYPE.stringof ~ statement;
     }
}

void main()
{
     mixin(Eval!int);
     static assert(is (typeof(var) == int));
}

Ali



More information about the Digitalmars-d-learn mailing list