#Coersion with `?noreturn`

1 messages · Page 1 of 1 (latest)

formal axle
#

Say I have a function func which will always return null. (my real use case is that it returns null or error, which gets coerces to !?u8 by the caller, but the error return is not relevant here).
func must be able to be evaluated at runtime: its behaviour can be dependant on a runtime-known argument.

I can express this as fn func(cond: bool) ?u8 which compiles. But this isn't obvious that func will never return a u8.

I was wondering why fn func(cond: bool) @TypeOf(null) is invalid?
Also, fn func(cond: bool) ?noreturn also doesn't work (for seemingly different reasons?).

pub fn main() !void {
    var cond = true; // runtime-known
    _ = &cond;

    const result1: ?u8 = func1(cond);
    _ = result1;

    const result2: ?u8 = func2(cond);
    _ = result2;
}

fn func1(cond: bool) @TypeOf(null) {
    _ = cond;
    return null;
}

fn func2(cond: bool) ?noreturn {
    _ = cond;
    return null;
}

For func2, I cant' see why ?noreturn can't be coerced to ?u8, since --removing the ?-- noreturn CAN be corced to u8.

Same behaviour on 0.15.2 and 0.16.0-dev.2471+e9eadee00

#

making cond comptime-known (or removing it) makes func1 work

#

i think i am asking two different questions here xD

ocean zephyr
#

?noreturn cannot be coerced to ?u8 because it’s not clear when it is noreturn, should it be coerced to the null case or the u8 case since both cases are valid, remember that noreturn can be coerced to any type

rancid tangle
ocean zephyr
#

welp, i stand corrected