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.
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
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:
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.