Thread overview
mixin break; in switch containing static foreach
Apr 03, 2018
Vladimirs Nordholm
Apr 03, 2018
Adam D. Ruppe
Apr 03, 2018
Alex
Apr 03, 2018
Vladimirs Nordholm
April 03, 2018
My base problem is that I want to mixin `break` into a switch statement, but the mixin is within a static foreach. Take a look at the following code:

    switch(foo)
    {
        static foreach(f; EnumMembers!Foo)
        {
            mixin(format("
                case Foo.%s:    bar = Bar.%s;    break;
            ", f, f));
        }

        default: /* do stuff */; break;
    }

The above code returns the error "Error: must use labeled `break` within `static foreach`". For a more complete example, take a look att this sample code: https://dpaste.dzfl.pl/f3ab6d9679fc

I also attempted this solution, but I got the error that the label didn't exist. (And it looks pretty ugly)

    mixin(format("%s: case Foo.%s: abc = Foo.X%s; break %s;", f, f, f, f));

Any of you have a solution for this problem?

Best regards,
Vladimirs Nordholm
April 03, 2018
On Tuesday, 3 April 2018 at 19:31:50 UTC, Vladimirs Nordholm wrote:
>     switch(foo)

Put the label on the switch

whatever: switch(foo)


>             mixin(format("
>                 case Foo.%s:    bar = Bar.%s;    break;
>             ", f, f));

then use the label here

break whatever;
April 03, 2018
On Tuesday, 3 April 2018 at 19:31:50 UTC, Vladimirs Nordholm wrote:
> My base problem is that I want to mixin `break` into a switch statement, but the mixin is within a static foreach. Take a look at the following code:
>
>     switch(foo)
>     {
>         static foreach(f; EnumMembers!Foo)
>         {
>             mixin(format("
>                 case Foo.%s:    bar = Bar.%s;    break;
>             ", f, f));
>         }
>
>         default: /* do stuff */; break;
>     }
>
> The above code returns the error "Error: must use labeled `break` within `static foreach`". For a more complete example, take a look att this sample code: https://dpaste.dzfl.pl/f3ab6d9679fc
>
> I also attempted this solution, but I got the error that the label didn't exist. (And it looks pretty ugly)
>
>     mixin(format("%s: case Foo.%s: abc = Foo.X%s; break %s;", f, f, f, f));
>
> Any of you have a solution for this problem?
>
> Best regards,
> Vladimirs Nordholm

Would labelling help?
https://run.dlang.io/is/vE1KyD

April 03, 2018
On Tuesday, 3 April 2018 at 19:41:54 UTC, Alex wrote:
> On Tuesday, 3 April 2018 at 19:31:50 UTC, Vladimirs Nordholm wrote:
>> [...]
>
> Would labelling help?
> https://run.dlang.io/is/vE1KyD

Ah! Okay, now I see.

Thanks Alex and Adam!