3 ms·
I'd argue that hiding what's going on for the sake of "simplicity" ends up confusing a novice more when it means the function ends up behaving inconsistently. E
by lmm 7y ago
I'd argue that hiding what's going on for the sake of "simplicity" ends up confusing a novice more when it means the function ends up behaving inconsistently. E.g. in many languages a novice might see a function that returns just "Response", but when they call it it actually "throws" an "exception", which is a whole different concept that you have to understand and that completely destroys the simple "function evaluates to its return value" model.
Result has the advantage of being a plain old datatype that's just written in normal code - it follows all the normal rules of the language, and the user can probably click through and read its implementation for themselves.
- ngrilly 7y agoI think the Result type is quite easy to understand, and it's central to Rust error handling strategy. The main difficulty is we also have to understand the purpose of the dyn keyword and why the Error must be boxed. Anyone writing serious Rust will have to learn this sooner or later, but maybe this is a lot to learn "upfront" :-)
- 0815test 7y ago> The main difficulty is we also have to understand the purpose of the dyn keyword and why the Error must be boxed. Box<dyn Trait> is just an owning opaque pointer to something that implements the Trait interface. This kind of stuff is done all the time in C/C++ because it's just so darn useful, even though it requires a bunch of boilerplate code to make it work properly. (It's also arguably implied in the semantics of other languages, even though it's not directly marked in the code.) In Rust, it's just a line of code and the compiler provides checks that ensure you're writing something reasonable. (And of course you can dispense with the whole boxing mechanism when it's not needed, same as in C.)