3 ms·
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
by gbear605 8y ago
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.
- uryga 8y agoAh, 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!
- Jtsummers 8y agoYou 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.