Conversation

Given the unwieldlyness of exceptions, perhaps what I would really want in an OOP language is a construct to say "return from this function, AND the calling function, and return value X from the calling function.".
6
12
Replying to
This gets tricky when you consider different return types. This either requires that functions don't declare a return type, or that all of them return some kind of "result" type.
1
Replying to and
You define a sum type with the possible errors and implement the From trait for implicitly converting from each. For example, #[derive(From)] enum MultipleError { One(ErrorOne), Two(ErrorTwo) }. You can use the ? operator (or the legacy try!) to return one of the variant types.
1
It doesn't involve invisible control flow through functions or unwinding. You can see every potential return directly in the source code. The syntactic sugar is very light was implemented as a macro, which can be user-defined too. The operator is just a short hand form of it.
1
Sum types are one of the core data types just like tuples (product types). They're very broadly applicable and widely used, far beyond error handling. It's simply natural to use a type resembling Either<A, B> for error reporting and Rust just has a standard type and idiom for it.
1
The syntactic sugar is something that arose naturally and was originally user-defined outside the standard library. It's a trivial macro encoding a common pattern in code: propagating errors upwards. It's so widely used that it was given an operator. Doesn't make it an exception.
1
Show replies