RFC: syntax: Implement for/while-then & labeled, multi-level break #60367
+994
−62
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This implements multi-level and labeled break as contemplated in #5334. In addition this adds support for break-with-value (#22891) as well as for-then (aka for-else #1289). Also while-then of course. All three features are syntax gated to 1.14 syntax.
The syntax for multi-level break is as follows:
The break value can be
continuein which case the next innermost loop is continued:For more deeply nested loops, the loop can be annotated with a
@labeland the the break or continue can be targeted using@goto:Naturally
continueis supported here as well.The syntax and semantics for
for-thenare as proposed in the issue:Any
break(including multi-level and labeled) skips the corresponding loop'sthenblock, which ordinarily would run at loop completion.I think the
thenkeyword deserves some bikeshedding. I think theelsekeyword is seen as a bit of a mistake in languages that have it. Common lisp usesfinallybut I wanted to avoid it here since the semantics are substantially different fromfinallyin a try-catch block (as mentioned in the original issue. Perl has a similar feature with acontinueblock (but it runs every time). Claude points out that django templates have anemptyclause in for loops that runs if and only if the collection is empty (but doesn't have break in any case).Another keyword options I thought about is
normally. Kind of likefinally, but for normal termination.An additional motivation here is to make multi-level
breakavailable as syntax for the potential future addition ofmatch(#60344).Written by Claude. Some remaining cleanup and todos, but appears to basically be working.