mirror of
https://github.com/ziglang/zig.git
synced 2024-11-15 00:26:57 +00:00
langref: Add test case for "if error union with optional"
This is an edge case that isn't too uncommon but is rather confusing to try to deduce without documentation, since it feels like `else` is being overloaded in this scenario and there's no obvious 'correct' behavior here. This just adds a test demonstrating how Zig currently behaves in this scenario.
This commit is contained in:
parent
eeae3a8f9d
commit
f77c968cf8
@ -3861,6 +3861,32 @@ test "if error union" {
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
|
||||
test "if error union with optional" {
|
||||
// If expressions test for errors before unwrapping optionals.
|
||||
// The |optional_value| capture's type is ?u32.
|
||||
|
||||
const a: anyerror!?u32 = 0;
|
||||
if (a) |optional_value| {
|
||||
assert(optional_value.? == 0);
|
||||
} else |err| {
|
||||
unreachable;
|
||||
}
|
||||
|
||||
const b: anyerror!?u32 = null;
|
||||
if (b) |optional_value| {
|
||||
assert(optional_value == null);
|
||||
} else |err| {
|
||||
unreachable;
|
||||
}
|
||||
|
||||
const c: anyerror!?u32 = error.BadValue;
|
||||
if (c) |optional_value| {
|
||||
unreachable;
|
||||
} else |err| {
|
||||
assert(err == error.BadValue);
|
||||
}
|
||||
}
|
||||
{#code_end#}
|
||||
{#see_also|Optionals|Errors#}
|
||||
{#header_close#}
|
||||
|
Loading…
Reference in New Issue
Block a user