Pre-RFC: Catching Functions

It's even more radical proposal than one in the OP. I think we don't get much from hiding Result type and it makes extension to Option and other types through try trait even more difficult. And hiding ? does not help either.

I think this syntax can be an acceptable middle ground:

try fn foo() -> Result<T, E> {
    let x: T = try_g()?;
    if fail() { throw e; }
    x
}

try fn bar() -> Option<T> {
    let x: T = try_g()?;
    if is_none() { throw; }
    x
}

try can be replaced with catch or something else, same with throw. This variant does not hide enums and makes integration with other types trivial through Try trait. And it allows Ok wrapping, through explicit notion of try fn, which with other modificators like async fn (and maybe others in future?) notifies user that this function operates under slightly different rules compared to the usual fn.

5 Likes