www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixin break; in switch containing static foreach

reply Vladimirs Nordholm <v vladde.net> writes:
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
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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
prev sibling parent reply Alex <sascha.orlov gmail.com> writes:
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
Apr 03 2018
parent Vladimirs Nordholm <v vladde.net> writes:
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!
Apr 03 2018