Re:Programming Paradigms for Dummies: What Every Programmer ...
by gbear605 on Monday November 05, 2018 @10:16AM
(#18382891)
A rather odd example with Result type systems:
Say you have a function that returns the results of three other functions together in a tuple and the first one errors. The other two can still run and their result can be returned in the tuple along with an Error for the first function.
[ Parent
| on HN ]
Re:Programming Paradigms for Dummies: What Every Programmer ...
by uryga on Monday November 05, 2018 @11:12AM
(#18383439)
Ah, so using Result types you can return
`(Optional<A>, Optional<B>, Optional<C>)`
– each can fail or succeed independently. But you can't do that if you're using exceptions to signal errors – it's like returning `Optional<(A, B, C)>`
, you get either all the results or nothing. Good example, thanks!
Re:Programming Paradigms for Dummies: What Every Programmer ...
by Jtsummers on Monday November 05, 2018 @12:20PM
(#18384029)
You can still get something like that using exceptions, it depends on where the exceptions are caught. If you have a function which has 3 calls:
function f (...) {
val a = something.a();
val b = something.b();
val c = something.c();
return (a,b,c);
}
If the exceptions can occur in the calls assigning to a, b, or c and are handled locally (here, in f) then you can construct that first case. It's only if you throw the exception up one level higher that you end up in the latter case. As written, it is more like your latter example. But with a modication: function f(...) {
val a = default_a;
val b = default_b;
val c = default_c;
try {
a = something.a();
} catch {
a = error_value_a;
}
...
}
You can get the former case.