Compile time foreach with switch

Johan Fjeldtvedt via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 21 12:09:25 PDT 2017


I was a bit surprised to find out 
(https://forum.dlang.org/post/csiwyetjkttlxxnwndif@forum.dlang.org) that compile time foreach-loops can be used inside switch-statements. I tried the following:

import std.stdio;
import std.typecons;

void foo(string s) {
     enum es = tuple("a", "b", "c");
     switch (s) {
       foreach (e; es) {
         case e:
             writeln("matched ", e);
             break;
       }
     default:
         writeln("no match");
         break;
     }
}

void main() {
     foo("a");
}

However, this prints both "matched a" and "no match". It seems 
like either the break or the default: label is ignored. In this 
example I could have used return of course, but is this behavior 
correct?


More information about the Digitalmars-d-learn mailing list