3 ms·
Obviously exceptions are vastly superior to sum types for errors. Compare the following very readable exception based-pseudocode: fn doSomethingFallibly()
by gpderetta 4y ago
Obviously exceptions are vastly superior to sum types for errors. Compare the following very readable exception based-pseudocode:
fn doSomethingFallibly() -> SameVal, except Error
SomeVal x = ...;
if failed:
raise Error
else
return x
fn doTheThing() -> void
try Someval x = doSomethingFallibly()
/* use x */
except Error: /* log the error */
To the utterly unreadable error variant based implementation that uses obscure functional stuff like patter matching:
fn doSomethingFallibly() -> SameVal | Error
SomeVal x = ...;
if failed:
return Error
else
return x
fn doTheThing() -> void
match doSomethingFallibly():
SomeVal x: /* use x */
Error: /* log the error */
This is especially egregious if you want to propagate the error. The exception based variant can focus on the success control flow:
fn doTheThing() -> void, raises Error
SomeVal x = try doSomethingFallibly()
While for the error variant case you have to use even more obscure monadic code:
fn doTheThing() -> void|Error
SomeVal x <- doSomethingFallibly()?
- red_admiral 4y agoHere's sum types in scala 3: for x <- fn1() y <- fn2(x) z <- fn3(y) yield z That is both readable and safe, here fn1-fn3 all return an Either[Error,T] (the error type has to be common but the success type can vary by function).
- js8 4y agoThe advantage of errors as return values is simplicity and localization of the impact. Each of the three major paradigms (return values, exceptions, error handlers) has pros and cons, neither is strictly superior.
- tialaramex 4y agoWas this missing a /s ? Poe's law applies so hard to people's language syntax preferences. By which I mean most of you are wrong, to be clear.
- gpderetta 4y agoCheck my other top level comment in this thread :).
- tialaramex 4y agoI confess I had a failed de-dupe bug on HN (last week I think?) and so I just assumed you fell victim to the same bug and didn't realise the posts are subtly different.
- gpderetta 4y agoFWIW, currently Errors winning vs Exceptions 6-1.
- red_admiral 4y agoAre you doing A/B testing by looking at the votes for each one? :)
- gpderetta 4y agoThat's really a side effect :). I just wanted to point out that given the right sugar and abstraction capabilities, the two models are really equivalent. BTW, Error is winning 7-6 at the moment :)
- gombosg 4y ago"Obviously" hilarious! :)