|
|
|
|
|
by andrewjf
3429 days ago
|
|
I think the most benefit of wrapping the result in an optional is so you can use compiler to enforce exhaustive pattern matching[1]. Optional<Integer> num = getSomeRiskyNumber();
match num {
Some(x) => ... ;
None => return error or whatever;
}
The compiler will verify you at least pretended to look at the None result instead of just ignoring it (but you could still swallow the none).1: does java even have this? |
|
With match, that cleans up a LOT of code and if the compiler can guarantee no speed or memory hits for doing this it becomes very valuable very quick.
It's one of the reasons I wanted to learn Rust.