Syntax for returning early with an error

Actually if one were to make enum variants first class types, then this special casing could be done cleanly, and even let x = Err(1); x? would be a terminator, as x would be inferred of type Result::Err instead of Result, and then Result::Err could implement a new "TryResidual" trait that would trigger the behavior.

I think this requires a new edition though as I think it's not backward compatible to make this inference change in general, but it might actually be a viable solution, perhaps (for older editions, macros or functions could be provided that produces the enum variant explicitly).

I think that with never_type we already got all we need. No need for anything fancy/new/involving-editions/etc…

#![feature(never_type)]

fn yeet<E>(e: E) -> Result<!, E> {
    Err(e)
}

fn foo() -> Result<i32, bool> {
    yeet(false)?;
    // unreachable
}

(playground)

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.