3 ms·
Monads are a superior form of error handling than exceptions... The only problem is that AFAIK (but I'm still learning it) Rust is missing something equivalent
by marcosdumay 10y ago
Monads are a superior form of error handling than exceptions... The only problem is that AFAIK (but I'm still learning it) Rust is missing something equivalent to Haskell's do notation.
- burntsushi 10y ago> The only problem is that AFAIK (but I'm still learning it) Rust is missing something equivalent to Haskell's do notation. Rust has essentially specialized do-notation for error handling called the `try!` macro, or more recently, `?`. (`try!` and `?` are exactly equivalent in today's Rust.) Actually, it does just a bit more than standard do-notation would: it also tries to convert your error value at the call site to the error type expected by the return type of the current function. The problems posed in the OP are pedagogical ones IMO that I hope can be solved. I think the current resource on error handling in the book is good for folks who really want to dive in and figure out the complete story, but it's bad for folks who just want to write code that works without spending a couple hours doing a deep dive. So I think there's room for more targeted pedagogy here.
- hota_mazi 10y ago> Monads are a superior form of error handling than exceptions No, they're not, not by a long shot. As soon as you start having multiple monads returned by functions and you need to combine them, you need to introduce monad transformers and your code turns into a giant untractactable spaghetti mess. Exceptions have issues but they are the sanest way to handle errors today.
- marcosdumay 10y agoWell, if you start doing more with monads than error handling, you will have complex interactions between them. They are still superior for error handling, exceptions would have a more complex interaction with them.
- placeybordeaux 10y agoI honestly found error codes in go to be pretty pragmatic. You pay a small cost on the writing side, but it's very clear and straight forward.