static code generation

Philippe Sigaud philippe.sigaud at gmail.com
Mon Dec 10 12:17:29 PST 2012


> If I'm not mistaken isn't the "code" I'm trying to generate still in a
> string?
>

Well, yes, but not when you mix it in. It's a string mixin in this case,
not a template mixin.


>
> (you've unfortunately left out the most important part at `//build your
> code here`)
>


Because it's just simple string operations using the entire
language/Phobos/whatever to get the code you want. And then, you mix it in.

Anonymous gave you http://dpaste.dzfl.pl/5d4cb742

With a string mixin, this gives:

http://dpaste.dzfl.pl/8fc32179

string genStruct(string stringname)
{
    return
    "struct " ~ stringname ~ "
    {
        //....
    }";
}

mixin(genStruct("s1"));
mixin(genStruct("s2"));

static assert(is(s1));
static assert(is(s2));

void main() {}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20121210/734466ee/attachment.html>


More information about the Digitalmars-d-learn mailing list