I like idea behind throw
, but T catch E
as a sugar for Result<T, E>
looks quite unnecessary to me and hides enum
nature of error handling, which I think will hurt learnability. The only case for which it looks a bit useful is when return type is Result<(), E>
, but I am not sure if it’s enough for such addition. Also as was noted by others usage of throw/catch will probably be a big source of confusion for newcommers. Same goes for Ok
wrapping, while I too don’t like Ok(())
that much, I don’t think that hiding it behind magic is a good idea, especially considering that this behaviour will be non-generic.
I would like to have instead two new keywords (lets call them reta
and retb
for now) which will work in conjunction with this trait:
trait RetTrait {
type A;
type B;
fn reta(val: Self::A) -> Self;
fn retb<V: Into<Self::B>>(val: V) -> Self;
}
impl<T, E> RetTrait for Result<T, E> {
type A = T;
type B = E;
fn reta(val: Self::A) -> Self {
Ok(val)
}
fn retb<V: Into<Self::B>>(val: V) -> Self {
Err(val.into())
}
}
So if we’ll write reta 1;
in case of Result
it will be sugar for return Ok(1);
and retb "error text";
will be sugar for return Err(From::from("error text"));
. And it can be easily extended to other types.