[phobos] expand for std.metastrings

kenji hara k.hara.pg at gmail.com
Sun Oct 31 08:50:47 PDT 2010


I wrote a trivial utility template for generating code string.
This provides some easiness and viewability of metaprogramming.
(This is currently used in my adaptTo.)

http://github.com/9rnsr/scrap/blob/master/expand/expand.d

Sample:
----
template GenFunc(string name)
{
  mixin(
    mixin(expand!q{
      int ${name}(){ return 10; }
    }));
  // result of expand! is:
  //   q{`int ` ~ name ~ `(){ return 10; }`}
}
mixin GenFunc!("test");   // generates function "test" returns int
unittest{
  assert(test() == 10);
}
----

expand!q{ codes... } parses D code string, and expands ${ expr } for
embedding expr that is expression evaluated as string in compile-time.

do you think?

Kenji


More information about the phobos mailing list