News for hackers. Stuff that matters.
Re:Elegant Error Handling with the JavaScript “Either” Monad by cousin_it on Wednesday June 19, 2019 @08:08AM (#20222534)
Agreed. I never understood the love for Either-based error handling in Haskell and other languages. It's such a bare bones system. If you add all the features that people want from an error handling system, like stack traces; and remove the misfeatures that nobody wants, like Either<IOError,Either<MathError,Result>> being a pointlessly different type from Either<MathError,Either<IOError,Result>>; you end up with (surprise!) try-catch.
Re:Elegant Error Handling with the JavaScript “Either” Monad by roblabla on Wednesday June 19, 2019 @08:37AM (#20222716)
I'm not familiar with Haskell, but Rust's `Result` type is the same idea, and I wouldn't want to trade it for try-catch. It supports backtraces[0], has a simple way to turn errors into a "catch-all" type[1], but it is significantly less complex since it reuses the basic return value mechanism a function has.

If you want to exit the program because the error can't be handled (similar to an unhandled exception), rust has a different concept for it, panics, and any Result can be turned into a panic via `unwrap()`.

IMO both are important: panics represent unrecoverable errors, while Results can be acted upon (for instance, if read() returns Err(EINTR), you'll want to retry the read)

[0]: Granted, you need to write some boilerplate to get the backtrace on error creation.

[1]: You can cast all errors to `Box<dyn Error>`, a heap-allocated dynamically dispatched Error type.

Re:Elegant Error Handling with the JavaScript “Either” Monad by kmonsen on Wednesday June 19, 2019 @08:54AM (#20222820)
So in rust you write with result and in js you use try/catch. Pretty simple, and readers will understand the code.
Re:Elegant Error Handling with the JavaScript “Either” Monad by roblabla on Wednesday June 19, 2019 @10:33AM (#20223696)
Oh I agree, and definitely don't recommend using Result in JS, the language is just not tailored for it. I was specifically addressing

> I never understood the love for Either-based error handling in Haskell and other languages.

Re:Elegant Error Handling with the JavaScript “Either” Monad by lmm on Wednesday June 19, 2019 @08:49AM (#20222791)
> If you add all the features that people want from an error handling system, like stack traces; and remove the misfeatures that nobody wants, like Either<IOError,Either<MathError,Result>> being a pointlessly different type from Either<MathError,Either<IOError,Result>>; you end up with (surprise!) try-catch.

What does a function that doesn't error look like under try-catch? How do you tell it apart from a function that can error.

It's the same argument as null/Optional. Yes, Optional behaves the same as a nullable value; the point is that languages with Optional allow you to have values that aren't nullable.

Re:Elegant Error Handling with the JavaScript “Either” Monad by cousin_it on Wednesday June 19, 2019 @09:42AM (#20223240)
> What does a function that doesn't error look like under try-catch? How do you tell it apart from a function that can error.

By the empty throws clause.

Re:Elegant Error Handling with the JavaScript “Either” Monad by lmm on Wednesday June 19, 2019 @09:57AM (#20223373)
Edit: Parent was edited, previously suggested "Java-style exception specifications". As far as I know every language with "throws clause"s has the kind of problems I describe below though.

Three major problems: they interact inconsistently with generics, you can no longer take a function's result and store it as a value, and you still can't tell whether a function might throw by looking at the point where it's called.

A good version of exception specifications would be lightweight syntax sugar over either (in the same way that a good version of async/await is a lightweight syntax sugar over futures/promises). But I'm not aware of any language implementing that; I guess once you have working Either there's not enough value in providing an alternative syntax for it.

Re:Elegant Error Handling with the JavaScript “Either” Monad by cousin_it on Wednesday June 19, 2019 @11:19AM (#20224122)
Actually I thought a bit and realized that I don't want to defend checked exceptions. Let me amend the previous answer to "I prefer such functions to be interchangeable".
Re:Elegant Error Handling with the JavaScript “Either” Monad by lmm on Wednesday June 19, 2019 @11:38AM (#20224288)
If those functions are interchangeable then you potentially have to deal with exception control flow on every line, or even within a single line. It makes it very hard to reason about all possible paths through a given function and do things like safe resource management (e.g. you always have to handle the possibility of further exceptions in your exception-handling code path).

For system failures it's ok because the only handling you're going to do is retry at high level (and so even languages with an Either-oriented style tend to have some form of "panics" or exceptions for that kind of failure). But it can be a real problem when that also happens for "normal failure" paths like bad user input where you want to actually have business logic that handles and perhaps recovers from the failure.

Re:Elegant Error Handling with the JavaScript “Either” Monad by cousin_it on Wednesday June 19, 2019 @02:20PM (#20225734)
I think you can often avoid reasoning about all possible paths, by having one try-finally block per each resource to be freed.
Re:Elegant Error Handling with the JavaScript “Either” Monad by 0815test on Wednesday June 19, 2019 @09:09AM (#20222934)
The types are definitely isomorphic, but the difference between them is not "pointless". You could have Either<IOError, Either<IOError, Result>> and want to preserve the information about which step led to an IOError outcome.
Re:Elegant Error Handling with the JavaScript “Either” Monad by cousin_it on Wednesday June 19, 2019 @09:58AM (#20223382)
That information shouldn't be in the type, because it makes functions pointlessly non-interchangeable. With try-catch, a superset of that information is preserved in the stack trace, and functions stay interchangeable.
Re:Elegant Error Handling with the JavaScript “Either” Monad by esarbe on Wednesday June 19, 2019 @12:49PM (#20224925)
I consider the error types to be a useful information that should absolutely be part of the method type.

If a method can fail on IO that's different than if it cannot fail. And the surrounding code has to account for that.

So, you might think that the methods are interchangeable if the error type is not present in the signature but this is actually wrong. Different failures have to be handled differently.

(I assume that you are /not/ talking about valid error cases in the problem domain that also might have to be handled)

Re:Elegant Error Handling with the JavaScript “Either” Monad by chii on Wednesday June 19, 2019 @09:01AM (#20222857)
why is either wrapped in eithers in your example? That feels like wrong code to me. Wouldn't it be Either<(MathError|IOError), Result>, and you pattern match the error or result?