optionals
optionals copied to clipboard
Option.inspect()
To reduce the number of unwrap()
calls, I suggest adding inspect()
.
unwrap()
is dangerous because it only works in combination with a matching if
.
if (value.isSome()) {
use(value.unwrap());
// ...
}
// turns into
value.inspect(value => {
use(value);
// ...
});
This example is currently doable using map()
value.map(value => {
use(value);
// ...
});
This works, but is misleading because map()
is a function with a return value that should not be discarded.
Rust's if let
is different from inspect()
in it can return from within the body.
Apologies for the long wait! Yes this is a lovely idea, I will make sure to add it to the next release.