3 ms·
I'm not sure that would be any better, in practice, than doing return 0, nil.
by sacado2 5y ago
I'm not sure that would be any better, in practice, than doing return 0, nil.
- lucian1900 5y agoIt would allow functions to be treated generically as returning error-able types. Since some functions even return 3 arguments where the third is the error, this isn't possible today. It would also allow defer-like syntax to be used on the value a function returns, to return early from the calling function. Rust's ? is an excellent example of this, allowing explicit delegation of error handling that is still very lightweight syntactically.
- kortex 5y agoFunction composition becomes a lot easier with Result/Option, because you can write functions in terms of assuming a "good" value (not nil/err or otherwise "bad") and then map over it. Or if we really wanna be fancy, flatmap/bind over it. Foo, err requires either that all receiving functions accept (foo, err), or manually nil-checking at every stage. Try/catch is the least composable, because deeper error types can "jump out" from any layer. Which means you have to keep wrapping catches, until you hit some base layer you can recover from. You get stack traces, but you lose type information along the way. Sum (result) and product (foo, nil) types preserve type information. Meaning you can operate more generically over it. Meaning less code duplication.