why no statements inside mixin teplates?

via Digitalmars-d digitalmars-d at puremagic.com
Fri May 12 12:22:05 PDT 2017


On Friday, 12 May 2017 at 08:47:32 UTC, visitor wrote:
> works for me :
> mixin template testBoilerPlate(alias arg, alias expected)
> {
>     auto doit = {
>         import std.format : format;
>         auto got = compute(arg);
>         assert(got == expected, "expected %s got 
> %s".format(expected, got));
>         return true;
>     }();
> }

nice hack, or should I say idiom? I definitely didn't think of 
that one, I though of  using a parameterized function which kinda 
similar. but this isn't what I'm looking for.I just want to do C 
style macro expansion. no function/lambda call at runtime, even 
if the optimizer inlines the lambda's. I can do this with plain 
mixin, but it's uglier.

example with mixin:
//-----------------------
string testBoilerPlate(alias arg, alias expected)()
{
     import std.format : format;

     return q{
         {
             import std.format : format;
             auto got = compute(%1$s);
             assert(%2$s == got, "expected %%s, got 
%%s".format(%2$s, got));
         }
     }.format(arg.stringof, expected.stringof);
}

unittest
{
     mixin(testBoilerPate!("000", 1));
}
//-----------------------


More information about the Digitalmars-d mailing list