by rectang · 4y ago · 0 comments
Sum types have lots of other benefits, but what I really care about is having Result types instead of exceptions or returned error codes where the error handling can be omitted. Even if you choose to "unwrap" or similar, having high confidence that you can audit a codebase and at least know that every handleable error known to the APIs has been checked, that's invaluable. (Panics which represent non-handleable errors notwithstanding.)
That only works if you remember to deal with the Result type itself, which is something you're likely to forget to do if you prone to forgetting to handle values when they come to you.
Realistically, if you are prone to forgetting to handle the state in your application then you will be prone to forgetting to consider values of all kinds, including the Result itself. That is the achilles heel of the Result type, leaving it to be far less useful than it seems like it should be on the surface.
A language that will fail to compile if you forget to handle all the states of your application sounds wonderful, but I don't know of any languages that have even come close to solving that problem. That is an excruciatingly hard problem to solve, and maybe isn't even solvable without human-level intelligence.
https://play.rust-lang.org/?version=stable&mode=debug&editio...
warning: unused `Result` that must be used
--> src/main.rs:7:5
|
7 | foo();
| ^^^^^^
|
= note: `#[warn(unused_must_use)]` on by default
= note: this `Result` may be an `Err` variant, which should be handled
The quick fix is to change `foo();` to `foo().unwrap();`, which panics if the call returns an Err variant. But seeing `unwrap` in the code lets me know where an error may occur. That's better than the silent failure we get in many other languages when a result code is left unhandled or a potential exception isn't wrapped in a `try` block.