Thread overview
mixin string evaluation
Jul 21, 2013
Martin Krejcirik
Jul 21, 2013
Ali Çehreli
Jul 21, 2013
Martin Krejcirik
Jul 21, 2013
Artur Skawina
July 21, 2013
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.


-- 
mk
July 21, 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

July 21, 2013
On 07/21/13 18:26, 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.

Not enough context; I'm not sure what exactly you want to achieve.

The above can simply be done like this:

   enum source = "TYPE var;";

   mixin template Eval(TYPE) { mixin(source); }

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

artur
July 21, 2013
On 21.7.2013 19:15, Ali Çehreli wrote:
>     string Eval()
>     {
>         string statement = source;
>         auto found = statement.findSkip("TYPE");
>         assert(found);
> 
>         return TYPE.stringof ~ statement;
>     }

Thanks, actually findSplit is what I need, but works like a charm. CTFE rox !


-- 
mk