3 ms·
> try! and '?' is just a syntactic convenience for passing error returns to the caller. Indeed. Because manually handling those are a pain in the butt. So they
by dmitriid 4y ago
> try! and '?' is just a syntactic convenience for passing error returns to the caller.
Indeed. Because manually handling those are a pain in the butt. So they made a shortcut. That still needs to be handled by someone up in the hierarchy. Exceptions in all but name.
> It's not even monad-like because
No one cares whether it's monads or not.
- moloch-hai 4y ago> Exceptions in all but name. But with added space and branch-predictor overhead in every call site, and runtime overhead in every call event!
- tialaramex 4y agoThe crucial thing about Try (the ? operator) is actually pretty easy to see if you look at the Trait which makes it go: The result of the branch method on Try is ControlFlow<Self::Residual, Self::Output> Try isn't stable, but ControlFlow is. They've reified the control flow! Rust has a type which represents the idea of a decision whether to return early. This is in my opinion pure genius, and it happened almost by accident. It seemed natural at first that the decision to return early is manifested directly in Result, but it isn't. That's the insight. Failure and returning early are distinct ideas, and we might just as easily want to return early in consequence of success as failure. In separating "Success versus Failure" from "Return early versus keep going" Rust gets a lot more value here than is encapsulated in C++ Exceptions.
- the_why_of_y 4y ago> Exceptions in all but name. In particular, checked exceptions. This is a very interesting article that discusses the isomorphism between checked exceptions and error return types, they ended up with checked exceptions where a function Foo that may throw must be invoked with "try Foo()" - very similar to Rust's ADT-based macro/syntax. https://joeduffyblog.com/2016/02/07/the-error-model/ https://joeduffyblog.com/2016/02/07/the-error-model/