why no statements inside mixin teplates?

visitor via Digitalmars-d digitalmars-d at puremagic.com
Fri May 12 01:47:32 PDT 2017


On Friday, 12 May 2017 at 00:20:13 UTC, سليمان السهمي (Soulaïman 
Sahmi) wrote:
> Is there a rational behind not allowing statements inside mixin 
> templates? I know mixin does accept code containing statements, 
> but using mixin is much uglier. so  I was wondering.
>
> example use case:
> //---------------------------------
> int compute(string)
> {
>     return 1;
> }
>
> mixin template testBoilerPlate(alias arg, alias expected)
> {
>     {
>         import std.format : format;
>         auto got = compute(arg);
>         assert(got == expected, "expected %s got 
> %s".format(expected, got));
>     }
> }
>
> unittest
> {
>     mixin testBoilerPlate("12345", 1);
>     mixin testBoilerPlate("00" ~ "0", 2 - 1);
> }
> //--------------------------------

On Friday, 12 May 2017 at 00:20:13 UTC, سليمان السهمي (Soulaïman 
Sahmi) 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;
     }();
}


More information about the Digitalmars-d mailing list