How to assign optional tag

This doesn’t seem to work:

pub fn main() void {
    var f: ?*u8 = null;
    f = @as(*u8, undefined);
    std.debug.print("value: {?}\n", .{f});
}

I also tried:

pub fn main() void {
    var f: ?*u8 = @as(*u8, undefined);
    _ = &f;
    std.debug.print("value: {?}\n", .{f});
}
$ zig run -O ReleaseFast opt.zig
value: null

I still don’t believe there is a way to set a non-null value for any ?T without simply setting it to a value of T. I’m also not convinced that it is necessary, I would be grateful if you could provide a use case.

I was wrong, I did not know you could do this. However, .? is still only acting as an assertion, I don’t think it can be used to strictly access “just the value” of foo, because that’s not what it does semantically.

1 Like