Going from string to identifier

Jean-Louis Leroy jl at leroy.nyc
Wed Feb 21 22:11:04 UTC 2018


Here's what I am trying to do:

mixin template MakeFun(string ID, int X)
{
   int mixin(ID)() { return X; }
}

mixin MakeFun!("one", 1); // int one() { return 1; }

Alas I get:
makefunc.d(3): Error: no identifier for declarator `int`
makefunc.d(3): Error: found `{` when expecting `;`
makefunc.d(3): Error: declaration expected, not `return`
makefunc.d(4): Error: unrecognized declaration

Is there a shorter way than building the entire function 
definition as a string mixin? As in:

mixin template MakeFun(string ID, int X)
{
   import std.format;
   mixin(format("int %s() { return %s; }", ID, X));
}

mixin MakeFun!("one", 1);



More information about the Digitalmars-d-learn mailing list