4 ms·
Error handling in Rust works this way, and Rust doesn’t have exceptions. (It has panicking, but that’s expressly designed not to be caught except at the thread
by chrismorgan 7y ago
Error handling in Rust works this way, and Rust doesn’t have exceptions. (It has panicking, but that’s expressly designed not to be caught except at the thread boundary.) For Rust, the pattern works really well. Since Rust 1.0, there have been additions to the language to improve ergonomics, like the ? postfix operator, which will return an error, or unwrap a successful value.
Fortunately, Rust didn’t go with Haskell’s foolish Either, Left and Right naming for its error handling type, but gave the enum and its variants meaningful names, obvious semantics and good methods to go with that, so that the Result type has two variants, Ok and Err.
I write plenty of Rust and plenty of JavaScript. I find that various patterns that work very well in Rust don’t work so well in JavaScript; sometimes because I care about performance (runtime and memory) and porting the pattern harms that; and sometimes because of how you can’t conveniently add arbitrary methods to a type like you can in Rust due to its trait system, and so you’d have to give up method call syntax in favour of a hodge-podge of function and method calls. (This becomes a problem with all dynamic languages I know of; this is perhaps most clearly seen in Python where functional programming constructs are second-class citizens, in part because the likes of map are a function rather than a method—well, and because of its lambda syntax.) Also because you’re often working on existing projects that use the normal JS way of doing things, rather than getting to build from scratch, which would more readily allow you extravagances like trying out error handling models unusual for the language.
- agentultra 7y agoWhat is foolish about the naming? It's a Bifunctor and while often used for error handling in programs it actually has nothing to do with error handling. The naming, afaik, comes from the correspondence to the logical disjunction operator and so the constructors name the Left side of the disjunction and the Right side of the disjunction. Seems rather sensible to me.
- jupp0r 7y agoIf you use it for error handling, it takes a lot of mental energy to remember which side is the happy path and which side is the error path. Maybe (no pun intended), people should have used Either to build a Result type, but as it stands, raw Either is used everywhere by convention, creating confusion.
- Gajurgensen 7y agoIt would be nice if there was a standard type alias for Either which explicitly labeled good/bad values. That being said, I don't think it takes that much energy to remember that the right is the good value. If you are comfortable with monads, just remember that monads must be parameterized over a single type, and for Either that will be the right type (because we must partially apply the type constructor with the left type to get it down to the correct form).
- seppel 7y ago> That being said, I don't think it takes that much energy to remember that the right is the good value. And then clever guys come and use left as the good value, because then want to retry on error (as you can see in this thread). Plus it takes quite some energy to discuss the, well, unfortunate naming.
- leshow 7y agoI'm not sure which definition you're referring to, but every one I've seen you'd be hard-pressed to use left as the 'good' value. The type's definition favours 'right' for Functor/Applicative/Monad/etc instances.
- seppel 7y agoI'm referring to this comment: https://news.ycombinator.com/item?id=20224097 https://news.ycombinator.com/item?id=20224097
- leshow 7y agoThere are zero implementations of Left being the Ok value, just this hypothetical put forward by another commenter. edit: I should specify that I mean when Either is used as a 'Result' type. There are plenty of other use cases you can use Either outside of Ok/Err values.
- 7y ago
- lalaithion 7y agoHaskell's Either isn't intended to be used solely as Left = Err and Right = Ok. It's supposed to work as an abstract concept of "one thing or another", and the language designers have avoided choices that would canonicalize it that way, such as introducing a MonadFail (Either String) instance. One example of why you would buck the trend would be to use of Left = Ok and Right = Err for a series of "retry" computations. Say you have a few functions of type Either a String, which all get progressively slower, but which work on progressively more advanced input. An example might be a series of regex engines, each of which run slower but handle more and more advanced lookahead syntax. You can then write something like: makeRegex :: String -> Either Regex ParseError makeRegex regexExpression = makeBasicRegex regexExpression >> makeLookaheadRegex regexExpression This will return a Regex using a simple parser if it can, and if that function fails (by returning a Right value!) it will go onto the next function and try that.
- mpfundstein 7y agoInteresting use case. I honestly only saw either up until now as a more advanced maybe
- steveklabnik 7y agoFun bit of history: Rust used to have Either. Eventually we added Result. At some point, we looked at all the code that existed (ahhh, the things you can do when you're young) and nobody used Either, only Result. Today, either lives on as a package: https://crates.io/crates/either https://crates.io/crates/either It gets a lot of downloads because it is actually used by a few popular libraries: https://crates.io/crates/either/reverse_dependencies https://crates.io/crates/either/reverse_dependencies
- remram 7y agoEither also lives on in the `futures` library, where it is used when you have multiple things that can happen in your asynchronous task and you want to stick to static types rather than returning a trait object. https://docs.rs/futures/0.1/futures/future/enum.Either.html https://docs.rs/futures/0.1/futures/future/enum.Either.html