On Thu, 17 Apr 2014 06:54:39 -0400, Timon Gehr <timon.g...@gmx.ch> wrote:
On 04/08/2014 05:14 PM, Steven Schveighoffer wrote:
On Mon, 07 Apr 2014 18:30:30 -0400, Matej Nanut <matejna...@gmail.com>
wrote:
Hello,
I don't understand why so many break statements are needed in this
construct:
immutable key = 3;
switch (key)
{
foreach (c; TypeTuple!(1, 2, 3, 4, 5))
{
case c: "Found %s!".writefln(c);
break;
}
break; // Default always gets executed without this break.
default:
"Not found %s :(".writefln(key);
break;
}
One after each case and another one after the foreach.
First, let me say this is a cool usage of compile-time foreach, I had
never thought of that.
...
I do this quite often.
You get a gold star then ;)
Second, I think the issue is likely a bug with the break exiting the
wrong scope.
No, this is expected behaviour. break and continue work in any foreach
statement. break always breaks the innermost breakable statement. (In
particular, it does not pair up with case statements.)
But should a foreach over a tuple a breakable statement? Basically, the
above seems to me it should be equivalent to:
case 1:
writefln("Found %s!", 1);
break;
case 2:
writefln("Found %s!", 2);
break;
...
The foreach should be gone once the foreach is executed at compile-time.
If the break breaks the foreach, why isn't just case 1 produced? That
would be an actual break in the foreach, no?
-Steve