optionals icon indicating copy to clipboard operation
optionals copied to clipboard

Option.inspect()

Open Cre3per opened this issue 1 year ago • 1 comments

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.

Cre3per avatar Mar 05 '23 15:03 Cre3per

Apologies for the long wait! Yes this is a lovely idea, I will make sure to add it to the next release.

OliverBrotchie avatar Jul 11 '23 12:07 OliverBrotchie