Zig Version
0.16.0
Steps to Reproduce, Observed Behavior, and Expected Behavior
In the following piece of code num works while the declaration of num2 has a compilation error (listed at the end).
const std = @import("std");
const e = error{ a, b };
pub fn main() !void {
const what: ?e!u8 = null;
const valid_what_works: e!u8 = what orelse 0;
const valid_what_fails = what orelse 0;
const num: u8 = valid_what_works catch 1;
const num2: u8 = valid_what_fails catch 1;
std.debug.print("{} {}\n", .{ num, num2 });
}
valid_what_works is explicitly typed as e!u8 while valid_what_fails is not explicitly typed, this seems to make the compiler wrongly evaluates what orelse 0 to comptime_int (instead of e!u8) as shown in the error below.
For this exact reason its not possible to make a ?e!u8 into a u8 in a single line: (what orelse 0) catch 1. This fails with the same comptime error as shown below
src/main.zig:11:39: error: expected error union type, found 'comptime_int'
const num2: u8 = valid_what_fails catch 1;
~~~~~~~~~~~~~~~~~^~~~~~~