okOrElse<E> method

Result<T, E> okOrElse<E>(
  1. E error()
)

Returns Ok with a present value, or computes an Err when absent.

error is not called when the option is present.

final id = const None<int>().okOrElse(() => 'no id'); // Err(no id)

Implementation

Result<T, E> okOrElse<E>(E Function() error) {
  return switch (this) {
    Some<T>(:final value) => Ok<T, E>(value),
    None<T>() => Err<T, E>(error()),
  };
}