A break inside a static foreach loop is interpreted as "breaking" the foreach loop. The static foreach doesn't have a scope and one cannot break it during compile time. Instead the break will work at runtime.
----
import std.stdio;
void foo(Args...)()
{
foreach (A; Args)
{
pragma(msg, A);
writeln("runtime ", A.stringof);
break;
}
}
void main()
{
foo!(int, long, float, double)();
}
----
int
long
float
double
runtime int
----
This is highly confusing, particularly when combined with switch (see issue 7835).
It would be best to deprecate the existing break and require a break with label instead.
Comment #1 by mathias.lang — 2016-09-21T17:16:26Z
I tend to disagree here. The break will happen at runtime and skip the other iterations of that loop, and to me that's exactly what it should do, even if it might sound confusing at first.
If anything, we could improve flow analysis to skip this specific code, but having its behaviour changed would be highly confusing.
Comment #2 by robert.schadek — 2024-12-13T18:43:59Z