-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Proposal: while with nullable unwrapping #357
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
A few notes:
|
I'd be very happy to see As for the error union types, you would only lose type safety in the else block, and only if you try to use the null value as the error value. A simple fix would be to only allow a In other words:
|
One idea which would solve the type safety issue is for error unions we could require the |
That would mean:
|
Exactly, and notice that it's a perfect XOR, e.g. you can't accidentally do something with a nullable when you meant to use an error union, or vice versa. |
(I started typing this when there were only 2 comments in this thread.) The Java way to treat When I look at code, I like to be able to know what the types of things are. If I see The above emotions are a big motivator for Zig's boolean-only rule in This is all relevant to this issue because I don't want there to be a syntax that works for two (or a finite number of) different types without being clear which one it's operating on.
Since starting to type this rant, I see some cool proposals being discussed that perfectly fit my readability objective. Here's a list of
And here's the
Here are some concerns:
|
This is status quo. One more syntax that was proposed:
|
Consider this code: var maybe_node = linked_list.first;
while (maybe_node) |node| {
// ...
maybe_node = node.next;
} You should be able to put the var maybe_node = linked_list.first;
while (maybe_node; maybe_node = node.next) |node| {
// ...
} Not sure how to solve this. |
In theory, you could evaluate the extra expression on the while loop in the scope of the block - where |
Right, but that would look incorrect. It would be the only place syntactically that you don't read left to right, top to bottom, to determine what's in scope. Here's another proposal: Instead of
have
and the Then the example becomes: var maybe_node = linked_list.first;
while (maybe_node) |node| : (maybe_node = node.next) {
// ...
} A loop to count to 10 looks like: var n: usize = 1;
while (n <= 10) : (n += 1) {
%%io.stderr.printf("{}\n", n);
} Maybe |
I actually really like this. And |
I quite like this as well,
For completeness, a regular do-while would use this, but you wouldn't be able to unwrap with a do-while.
|
I'm not a fan of do...while, because it's barely different than: while (true) {
if (condition) break;
} I feel like do...while is trying to bend the syntax over backwards to accommodate what can be accomplished with a more explicit control flow. |
Just to throw in the ol' josh-style super verbose but pretty readable and correct syntax proposal: while (a) continue: (b) {}
while (a) |b| continue: (c) {} here it looks like you're declaring a label called I'm not a fan of the while-do proposal, because it looks like the do clause is what gets executed every iteration. similar to Bash's |
Old: ``` while (condition; expression) {} ``` New: ``` while (condition) : (expression) {} ``` This is in preparation to allow nullable and error union types as the condition. See #357
I agree this |
Now that const result = while (bar()) |value| {
if (value > 50)
break value;
} else {
10
}; |
Consider the following snippet:
Iteration continues until the guard is not
null
. If it isn't, the unwrapped value gets assigned to the variable between|
.The text was updated successfully, but these errors were encountered: