Options, Enums and Nullability

To @LucasSantos91 point, the semantics of optionals is that a value is either a valid T or null. Trying to add the third type ruins the semantics of being optional.

using the example from the langref you cite, why can’t the signature be like this? (Per @n0s4 suggestion)

const std = @import("std");

const Foo = ?f32;

pub fn main() void {
    var f: Foo = null;
    bar(&f);
    std.debug.print("value: {}\n", .{f.?});
}

fn bar(f: *Foo) void {
    f.* = 12.34;
}

Why do we need to know it’s not null before we assign to it?

3 Likes