-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
"continue: label" syntax for while loop continuation expression #472
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
When this first came up, the while loop was closer to the for loop in C - all that was missing was the initializer part of it. Both of them had the condition expression and iteration expression. These three were equivalent:
When the idea of making while loops handle nullables appeared, we needed a way to refer to the unwrapped value from inside the iteration expression, but keep a logical visual order of declarations, ie
See #357, @thejoshwolfe proposed something similar with the continue keyword/label I do agree though, the ({}) is pretty darn ugly and the syntax is strange, but it does sort of appear somewhere else - labels and the currently proposed named/labeled blocks. I intended to propose something that made this explicit, where common interation blocks were labeled and defined how the loop should iterate:
But I'm still not sure where we're going with the labeled blocks and gotos, I felt it would be hard to implement efficiently and in the end I managed to solve problem with regular functions. |
Agreed.
Actually no. The syntax was chosen with this consideration in mind. Please point out if this is actually possible, but I don't think it is.
Well, a simpler form is this, which is far less ugly: {var i: usize = 0; while (i < arr.len) : (i += 1) {
// ...
}} The But see also, the first point about this syntax being generally out of place in Zig.
This is very debatable. If source order is to parallel execution order, then yes, it belongs at the end. But that kind of reasoning also leads to reverse polish notation, and by that point we've lost readability. Another argument is that the continuation expression belongs close to the condition, because the two are closely related, and together they define what the loop is. (However, I couldn't come up with a real example illustrating this in the 20 minutes i spent just now trying to think of one.) |
I know this issue has been closed for a long time, but does zig need the extra while loop syntax in the first place? Can't this be done pretty simply and clearly using How is
any different from
|
These have the same behavior for continue, but have different behaviors if you exit the loop via var i: u32 = 0;
while (i < 10) : (i += 1) {
if (i % 2 == 0) continue;
if (i == 7) break;
std.debug.warn("iteration {}\n", .{i});
}
assert(i == 7); var i: u32 = 0;
while (i < 10) {
defer i += 1;
if (i % 2 == 0) continue;
if (i == 7) break;
std.debug.warn("iteration {}\n", .{i});
}
assert(i == 8); |
|
Ah, I can see how that would cause some nasty surprises if |
Current documentation mentions novel feature of
while
:Seeing it I was confused, because:
({...})
is ugly.for
) is placed at the very beginning.My suggestion is to use
continue:
label to implement this:The label would be allowed only inside while loop, at its very end, no continue would be allowed in the block, there would be max 1 such label permitted in while body, it would not be possible to explicitly jump at it or inside it.
Possibly this feature should be forbidden in case that there are less than 2 continue statements in the loop, to keep people from misusing it.
The text was updated successfully, but these errors were encountered: