Templates and stringof...
Philippe Sigaud
philippe.sigaud at gmail.com
Sat Aug 4 04:35:45 PDT 2012
On Sat, Aug 4, 2012 at 10:53 AM, David Nadlinger <see at klickverbot.at> wrote:
> example:
>
> ---
> mixin template BitfieldsOn(alias target, <…>) if
> (isIntegral!(typeof(target))) {
> mixin({
> string code;
> // Generate code using "target" as identifier.
> return code;
> }());
> }
>
> mixin BitfieldsOn!(foo, <…>);
> ---
Yes, I recently rediscovered this trick of having a mixin integrate an
anonymous delegate where code-as-string is generated. This is a very
nice interaction between different parts of D. This should be
explained somewhere, as it's a tool anyone using templates to do
metaprogramming should be aware of.
I'll update my tutorial on templates.
Last example in line, I needed to have a gensym, a 'fresh' symbol,
guaranteed not to be aleady defined in the local scope. That is, user
code uses:
mixin(gensym)
to get a fresh identifier. D templates are becoming powerful enough
that something like Lisp macros is becoming possible and, hence,
hygiene problems. (Hygiene, for those reading this, is having a
template use an already-defined variable and breaking local code, it's
not a quip on the personal habits of Lisp hackers)
Anyway, so, this gensym uses the exact same pattern.
More information about the Digitalmars-d
mailing list