by rectang · 4y ago · 0 comments
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.