www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixin string evaluation

reply Martin Krejcirik <mk-junk i-line.cz> writes:
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
Jul 21 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jul 21 2013
parent Martin Krejcirik <mk-junk i-line.cz> writes:
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
Jul 21 2013
prev sibling parent Artur Skawina <art.08.09 gmail.com> writes:
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
Jul 21 2013