digitalmars.D.learn - mixin break; in switch containing static foreach
- Vladimirs Nordholm (23/23) Apr 03 2018 My base problem is that I want to mixin `break` into a switch
- Adam D. Ruppe (6/10) Apr 03 2018 Put the label on the switch
- Alex (4/28) Apr 03 2018 Would labelling help?
- Vladimirs Nordholm (3/8) Apr 03 2018 Ah! Okay, now I see.
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
Apr 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;
Apr 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 NordholmWould labelling help? https://run.dlang.io/is/vE1KyD
Apr 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:Ah! Okay, now I see. Thanks Alex and Adam![...]Would labelling help? https://run.dlang.io/is/vE1KyD
Apr 03 2018