|
|
|
|
|
by jonhohle
225 days ago
|
|
I also don’t understand the argument that Result is anything other than a syntactic difference between these ideas. final Foo x;
try {
x = foo().bar().baz().car();
} catch (Exception e) {
x = null;
}
return Optional.of(x);
vs. let x = foo()?.bar()?.baz()?.car()?;
Some(x)
Both eat the error. Both wrap the value. The rust is more terse, but the meaning is the same. |
|
The difference is that Result is a value, which can be stored and operated on like any other value. Exceptions aren't, and need to be propagated separately. This is more apparent in generic code, which can work with Result without knowing it's a Result. For example, if you have a helper that calls a callback in parallel on every element of an array, the callback can return Result, and the parallel executor doesn't need to care (and returns you an array of results, which you can inspect however you want). OTOH with exceptions, the executor would need to catch the exception and store it somehow in the returned array.