Options, Enums and Nullability

Actually, in Debug mode, this works for all types T.

fn bar(comptime T: type) !void {
    var x: ?T = null;
    x = @as(T, undefined);
    try expect(x != null);
}

But in ReleaseFast, it fails for pointers and function pointers, because the undefined value for those types at runtime is (not guaranteed to be, but is usually) 0, which is the same as the null value. But I think this code should work for all types, so this is a language design flaw.

1 Like