-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
labeled loops, labeled break, labeled continue #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I added the ability for |
The proposed
|
(1) looks good to me. |
There are several useful use cases for
Here goto is more natural than other constructs. Using several named sub-functions to implement these blocks would decrease readability, especially if they need many parameters. Large functions are not inherently bad, if they can be read (mostly) linearly. IMO PS: GCC also has "computed goto" feature, which is used to implement fast interpreters (faster than those having big |
I think this discussion would benefit from real actual examples of using goto in actual code. It's easy enough to come up with pseudo code that demonstrates a desire for goto, but it may be the case that every real usecase for goto could be better implemented without it. I'm still very open minded to this feature, but i think it's time to bring in real examples.
I read some details about this here: http://eli.thegreenplace.net/2012/07/12/computed-goto-for-efficient-dispatch-tables Looks like the benefits come from two sources: skipping the bounds check on switch, and doing the jump in each case instead of jumping to a centralized dispatch instruction. Both of these optimizations are theoretically possible without computed goto in Zig, but achieving the same performance may require writing an optimization pass for llvm. I haven't done any research to see if this already works or not. Skipping the bounds check can be enabled by making the default case Putting the dispatch jump in each case isn't as obvious how to do, but this is equivalent to inlining something about the loop and switch. It's definitely possible that the right kind of compiler optimization could achieve this. |
The only use of I think it's just a labeled break from the for loop though. I'll throw in 2 use cases for goto:
|
Here's another one: mixing inline assembly and zig. If we had better inline assembly integration, we could have labels declared in assembly that we can use |
Here's another idea instead of labeled blocks. We already have the feature that Here's my proposal:
while (true) {
const while_loop = this;
return :while_loop;
} Now we don't have this awkward choice for functions, do we use |
Ad computed goto - the only use I am aware is in Forth intepreters, where it usually claims large (tens of %) speedups. The speedup is due to better fit for the branch prediction mechamism, as I understand it. I personally never used it. I use goto for error handling at the end of function, occasionally to jump down to a block. So far I avoided jumping up. I also used gotos hidden in macros to implement finite state machines (this results in ideal syntax). I never had problems with break and continue. In fact I see the C syntax for control flow as almost perfect and would not recommend to remove anything. Labeled break, perhaps, if only down. Return is IMO important visual clue. The proposal above (return instead of break) feels strange. |
I just want to voice my agreement with @PavelVozenilek - that return syntax feels strange. In terms of readability, we are leveraging some of the fundamental constructs of C to make the language more familiar and accessible to users, it feels strange to remove break and change return so much. The points about porting are also very legitimate, in order to replace C, we must first replace it =P |
We can also keep |
Goto is used in visual novels all the time. You rarely need to go back when reading a book, you only go forward. Functions return back, goto doesn't. You guys are making life harder for yourself for no good reason. Also, goto is good for error handling. It's used in linux kernel for that quite often. Defer seems to partially replace that usage, but I'm not sure if it can replace goto completely. It's also sometimes used to give a function two different exits. Another example here, http://ollydbg.de/ , Disasm.zip , assembl.c . I don't quite understand what exactly this code does, but I think it's useful here. Goto is a good lightweight replacement for exceptions, but using it for that is complicated. Need to make sure you don't have anything important on stack (no destructors), need to store correct stack pointer somewhere (to free a lot of stack space at once). If you goto inside a function, it's much easier, because the difference in stack before and after goto is always constant. Exceptions are good for avoiding a lot of conditional branches in the program and annoying checks, it must be affecting both program speed and program complexity. It's especially good for file parsing, when something can go wrong at literally any moment. They are pretty much multi-function gotos. Also read this #578. I think I need exceptions after all. Just don't need to abuse them. |
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 label
andcontinue label
, like in Java.goto label
for this kind of label.label: { ... }
goto label
to 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
goto
are effectivelycontinue
s; one of them would need to be a labeled continue; the other two can be easily converted to status quocontinue
s. 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
goto
by using labeledbreak
andcontinue
(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
return
from its function, which every usecase for labeled blocks I can think of would do.The real-world usage of
goto
is pivotal to this proposal. I don't want to act on this proposal without more real-world data.The text was updated successfully, but these errors were encountered: