News for hackers. Stuff that matters.
Re:Elegant Error Handling with the JavaScript “Either” Monad by chrismorgan on Wednesday June 19, 2019 @09:19AM (#20223034)
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.

Re:Elegant Error Handling with the JavaScript “Either” Monad by lalaithion on Wednesday June 19, 2019 @11:17AM (#20224097)
Haskell'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.
Re:Elegant Error Handling with the JavaScript “Either” Monad by steveklabnik on Wednesday June 19, 2019 @01:21PM (#20225207)
Fun 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 It gets a lot of downloads because it is actually used by a few popular libraries: https://crates.io/crates/either/reverse_dependencies

Re:Elegant Error Handling with the JavaScript “Either” Monad by remram on Wednesday June 19, 2019 @03:48PM (#20226670)
Either 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

Re:Elegant Error Handling with the JavaScript “Either” Monad by ubertaco on Wednesday June 19, 2019 @04:13PM (#20226886)
A use-case I've had for a quick right-biased Java Either I threw together is handling a migration from java.util.Date to java.time.LocalDate:

    List<LocalDate> dobs = users.stream()
        .map(person -> Either.either(person::getDeprecatedDob, person::getDobLocalDate))
        .map(oldOrNewFormat -> oldOrNewFormat.fold(
            (Date oldFormat) -> convertOldFormatToNew(oldFormat),
            (LocalDate newFormat) -> newFormat,
         ))
        .collect(Collectors.toList());
I use the right-biased-ness of the Either to prefer the new format, if available, but failing that, use the old format and then convert as needed.
Re:Elegant Error Handling with the JavaScript “Either” Monad by james-mcelwain on Wednesday June 19, 2019 @04:33PM (#20227048)
This could be accomplished using Optional#orElseGet without any additional library dependencies, though.
Re:Elegant Error Handling with the JavaScript “Either” Monad by ubertaco on Thursday June 20, 2019 @10:14AM (#20233127)
That's true. I didn't think of doing that. Nice!
Re:Elegant Error Handling with the JavaScript “Either” Monad by im3w1l on Wednesday June 19, 2019 @01:44PM (#20225370)
This sounds clever at the expense of readability. Creating an explicit fallback mechanism for a Result would imo be preferable.
Re:Elegant Error Handling with the JavaScript “Either” Monad by pwm on Wednesday June 19, 2019 @03:33PM (#20226554)
It's not clever for its own sake. Haskell is rooted in math and Either is logical disjunction. Its semantics is precisely that. Error handling is a specific use-case that can be implemented with Either. Rust as a language is more pragmatic. In real-world industrial code the concept of Either is predominantly used for error handling and thus Rust named their data constructors aptly.
Re:Elegant Error Handling with the JavaScript “Either” Monad by mpfundstein on Wednesday June 19, 2019 @11:47AM (#20224374)
Interesting use case. I honestly only saw either up until now as a more advanced maybe
Re:Elegant Error Handling with the JavaScript “Either” Monad by agentultra on Wednesday June 19, 2019 @09:24AM (#20223075)
What 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.

Re:Elegant Error Handling with the JavaScript “Either” Monad by jupp0r on Wednesday June 19, 2019 @12:23PM (#20224690)
If 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.
Re:Elegant Error Handling with the JavaScript “Either” Monad by Gajurgensen on Wednesday June 19, 2019 @03:12PM (#20226377)
It 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).

Re:Elegant Error Handling with the JavaScript “Either” Monad by seppel on Wednesday June 19, 2019 @04:28PM (#20227005)
> 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.

Re:Elegant Error Handling with the JavaScript “Either” Monad by leshow on Wednesday June 19, 2019 @06:30PM (#20228000)
I'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.
Re:Elegant Error Handling with the JavaScript “Either” Monad by seppel on Wednesday June 19, 2019 @07:05PM (#20228230)
I'm referring to this comment: https://news.ycombinator.com/item?id=20224097
Re:Elegant Error Handling with the JavaScript “Either” Monad by leshow on Wednesday June 19, 2019 @07:09PM (#20228253)
There 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.

Re:Elegant Error Handling with the JavaScript “Either” Monad by leshow on Wednesday June 19, 2019 @06:28PM (#20227986)
Right is a synonym for 'correct'. How much mental energy does that take?