Mixin operator 'if' directly

bauss jj_1337 at live.dk
Sat Dec 22 10:11:23 UTC 2018


On Saturday, 22 December 2018 at 03:44:09 UTC, Timoses wrote:
>
> Awesome hack!
> Being a hack, it would be even nicer if it worked ouf of the 
> box:
>
>     mixin template foo(bool b)
>     {
>         int _impl() { writeln(b); return int.init; }
>         int _ipml2 = _impl();
>     }
>
> vs
>
>     mixin template foo(bool b)
>     {
>         writeln(b);
>     }

I think this is the closest we can come to it:

mixin template Execute(alias p)
{
     auto __execute__()
     {
         p();

         return 0;
     }
     auto __unused__ = __execute__();
}

mixin template MyTemplate(int a)
{
     mixin Execute!({
         import std.stdio : writeln;
         writeln(a);
     });
}

void main()
{
     mixin MyTemplate!10;
}


More information about the Digitalmars-d-learn mailing list