break while; break for; break foreach

Steven Schveighoffer schveiguy at gmail.com
Thu May 13 19:36:28 UTC 2021


On 5/13/21 1:34 PM, sighoya wrote:
> On Thursday, 13 May 2021 at 16:55:18 UTC, Steven Schveighoffer wrote:
>> 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`?
> 
> I think yes. What about lexical non-local returns?

D doesn't have that I think. It would be a much bigger change.

> 
>> 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.
> 
> The problem is if you have nested ifs, whiles or fors, wouldn't it be 
> better then to annotate them and likewise the break/continue/return 
> keyword?

D already does this, you can break using a label. The problem I've had 
with it is that a label cannot be repeated within a function, so it's 
hard to write generic code which requires this feature.

It may be that we have to require labeling when there is an ambiguity.

> Kotlin does it similar: 
> https://blog.mindorks.com/learn-kotlin-returns-jumps-labels

This is almost exactly how D handles it today (except the 
return-to-label feature)

-Steve


More information about the Digitalmars-d mailing list