-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Proposal: Multi-object if
#8351
Comments
It doesn't add much to me, it's arguably less readable and adds a way of doing things and a thing to know about the langage |
This is pure syntax sugar, which I might add will make it easier in most cases to do the wrong thing. (When are you ever going to want to check multiple optionals and nothing else?) I don't think you quite understand the philosophy of Zig. We do not add features just for the sake of it, or for symmetry or whatever -- only ever to satisfy some concrete use case, or to make it easier to do things correctly. This is neither. |
@Mouvedia, If restricted to optionals only, then this proposal would be an improvement in readability, IMO. But the layer of syntax sugar is pretty thin and the use case pretty rare to begin with, so all in all this seems to be more trouble than it's worth. |
It seemed pretty inconsistent to me. With so many 👎 Ill just close it. |
I have an example where this could be pretty useful, but I'm also not sure if it's common enough that it would be a good idea to add it to the language. test "currently - first way" {
const opt1: ?i32 = null;
const opt2: ?i32 = null;
if (opt1) |o1| {
if (opt2) |o2| {
// <DO SOMETHING WITH: o1 and o2>
} else {
// This could potentially be a big block and copying it would be inconvenient:
// <ELSE CODE>
}
} else {
// This is the same block as the <ELSE CODE> above.
// <ELSE CODE>
}
}
test "currently - second way" {
const opt1: ?i32 = null;
const opt2: ?i32 = null;
blk: {
if (opt1) |o1| {
if (opt2) |o2| {
// <DO SOMETHING WITH: o1 and o2>
break :blk;
}
}
// <ELSE CODE>
}
}
test "with this proposal" {
const opt1: ?i32 = null;
const opt2: ?i32 = null;
if (opt1, opt2) |o1, o2| {
// <DO SOMETHING WITH: o1 and o2>
} else {
// <ELSE CODE>
}
} Using
|
@YohananDiamond test "currently - third way" {
const opt1: ?i32 = null;
const opt2: ?i32 = null;
var success = false;
if (opt1) |o1| if (opt2) |o2| {
success = true;
// Do something with o1 and o2
}
if (!success) {
// Big block of shared else code
}
} Maybe not the most robust of solutions, but doesn't require new syntax and can be easily adapted to more complex control flow e.g., if you need to test a boolean, an optional and an error at the same time, while this proposal would only allow composing multiple optionals. |
In #7257, which has been accepted, @thejoshwolfe says:
status quo
proposal
It's pretty much straightforward to me, but Ill add that it is obviously a AND (not a OR) so you will reach the
else
if at least one of the two isnull
.Of course the order will have to be matched for the else captures:
The only downsides that I see is if the
if
body is huge it will require a back and forth to check the order which can induce errors but on the other hand it will also promote the good practice of refactoring your code into smaller chunks (so that you see both theif
and theelse
).The text was updated successfully, but these errors were encountered: