Compile time foreach with switch

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 21 12:17:59 PDT 2017


On Fri, Apr 21, 2017 at 07:17:32PM +0000, Adam D. Ruppe via Digitalmars-d-learn wrote:
> On Friday, 21 April 2017 at 19:09:25 UTC, Johan Fjeldtvedt wrote:
> > void foo(string s) {
> >     enum es = tuple("a", "b", "c");
> >     switch (s) {
> >       foreach (e; es) {
> >         case e:
> >             writeln("matched ", e);
> >             break;
> >       }
> 
> Let me remove some surrounding stuff and ask you what happens:
> 
>        foreach (e; es) {
>          if(s == c) {
>              writeln("matched ", e);
>              break;
>          }
>        }
> 
> 
> What does that break do?
> 
> 
> Then realize that it does exactly the same thing when it is inside a
> switch... it always breaks the innermost thing, which happens to be
> this loop. (note that cases are not `breaked`, the switch is.)

Yeah, you want to label your switch statement, say `mySwitch:`, so that
you can do a `break mySwitch;`


T

-- 
Knowledge is that area of ignorance that we arrange and classify. -- Ambrose Bierce


More information about the Digitalmars-d-learn mailing list