-
-
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
when multiple union fields share the same type, allow them to share a body in a switch prong #1107
Comments
It should be possible to omit the And if the unioned types are unique, I may prefer to use them:
|
|
The above behavior works now but there might be a soundness issue here as the following works too: const Foo = union(enum) {
One: i32,
Two: bool,
Three: i32,
};
fn bar(f: Foo) void {
switch (f) {
.One, .Two, .Three => |x| {
@import("std").debug.warn("{}\n", x);
},
}
}
test "" {
bar(Foo{ .One = 21 });
bar(Foo{ .Two = false });
bar(Foo{ .Three = -1 });
} |
Hmm yes that looks like a missing compile error. |
It's not unsound, |
Yeah. The type of |
This has bitten me before. When I see |
then would this not be allowed? fn bar(f: Foo) void {
switch (f) {
.One => |x| {
@import("std").debug.warn("{}\n", x);
},
// tag types don't match
else => |x| {
@import("std").debug.warn("{}\n", x);
},
}
} |
I would say it should not be allowed, because the My intuition is that |
would that be a compiler error then? |
Yep, a compiler error would make sense. Something along these lines
|
on second thought i think the existing behavior is correct for switch (kernel32.GetLastError()) {
ERROR.SHARING_VIOLATION => return error.SharingViolation,
ERROR.ALREADY_EXISTS => return error.PathAlreadyExists,
ERROR.FILE_EXISTS => return error.PathAlreadyExists,
ERROR.FILE_NOT_FOUND => return error.FileNotFound,
ERROR.PATH_NOT_FOUND => return error.FileNotFound,
ERROR.ACCESS_DENIED => return error.AccessDenied,
ERROR.PIPE_BUSY => return error.PipeBusy,
ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong,
else => |err| return unexpectedError(err),
} |
An alternative, if capturing an const err = kernel32.GetLastError(); // store in `e` rather than using `|err|`
switch (err) {
ERROR.SHARING_VIOLATION => return error.SharingViolation,
// ...
else => return unexpectedError(err),
} It would still be confusing to me if |
The fact that I do think it should be a compile error on other switch prongs, however, when they do not share the same type. |
The text was updated successfully, but these errors were encountered: