break while; break for; break foreach

Steven Schveighoffer schveiguy at gmail.com
Thu May 13 16:55:18 UTC 2021


I've had a few occasions where I have to label a loop that contains a 
switch statement (or vice versa), and then write `break label;` in order 
to break out of the outer construct.

Wouldn't it be nice to just have this implicit, and allow `break 
keyword;` where keyword is one of `for`, `while`, `foreach` and even 
`switch`?

The labeling thing has drawbacks as well -- you can only have one label 
of a given name in a function, making it hard when you have compile-time 
loops that generate the same code with the same label (I've had to deal 
with this by enclosing each branch in it's own immediately-called lambda).

Does this make sense as a language feature? I believe it doesn't break 
(no pun intended) any code since you currently can't have a keyword in 
that position.

Could also allow `continue keyword` though that can be dicey, since 
continue doesn't work for `switch`, and you would have to have 2 
different loops to make it reasonable. But for consistency, you could 
have it.

-Steve


More information about the Digitalmars-d mailing list