Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.2k
labeled loops, labeled break, labeled continue #346
Copy link
Copy link
Closed
Labels
acceptedThis proposal is planned.This proposal is planned.breakingImplementing this issue could cause existing code to no longer compile or have different behavior.Implementing this issue could cause existing code to no longer compile or have different behavior.enhancementSolving this issue will likely involve adding new logic or components to the codebase.Solving this issue will likely involve adding new logic or components to the codebase.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.
Milestone
Description
Metadata
Metadata
Assignees
Labels
acceptedThis proposal is planned.This proposal is planned.breakingImplementing this issue could cause existing code to no longer compile or have different behavior.Implementing this issue could cause existing code to no longer compile or have different behavior.enhancementSolving this issue will likely involve adding new logic or components to the codebase.Solving this issue will likely involve adding new logic or components to the codebase.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.
EDIT: scroll to current proposal here: #346 (comment)
Proposal:
Remove the current goto and label constructs. Introduce two kinds of labels:
label: while (...,label: for (...break labelandcontinue label, like in Java.goto labelfor this kind of label.label: { ... }goto labelto enter the label.label: return foo()or similar without a block, but this may be confusing when labeled loops are a distinct concept. You could always just dolabel: { return foo(); }anyway, so we probably don't need a blockless form.I searched the Zig standard library as well as @andrewrk's tetris and clashos projects, and the only 3 uses of
gotoare effectivelycontinues; one of them would need to be a labeled continue; the other two can be easily converted to status quocontinues. I think those 3 pieces of code would be improved by this proposal.I think there's a legitimate usecase for labeled blocks, and I'd like to see if I can write some Zig code that needs them. My idea so far is some kind of tokenizer (XML parser perhaps?). It's always possible to avoid
gotoby using labeledbreakandcontinue(see Java), but I think there are cases where it would be an abuse of the syntax to do so.My rationale for proposing that control flow must not fallthrough into or out of a labeled block is to prevent accidents. My idea of a labeled block is a piece of code that exists outside the normal control flow of the function that you want to be able to jump into and out of with special control flow. In this sense, it's a more like a function than like a ... whatever an arbitrary label in the middle of code is. An important difference from a function is that a labeled block can
returnfrom its function, which every usecase for labeled blocks I can think of would do.The real-world usage of
gotois pivotal to this proposal. I don't want to act on this proposal without more real-world data.