Maybe<T>.wrap constructor

Maybe<T>.wrap(
  1. T compute()
)

Compute a value using compute and wrap the result in a Maybe.

compute is called to compute the value. If compute returns normally, a Maybe holding a value is created.

If compute throws an exception a Maybe holding an error is created using Maybe.error.

Implementation

factory Maybe.wrap(T Function() compute) {
  try {
    return Maybe(compute());
  }
  catch (e, trace) {
    return Maybe.error(e, trace);
  }
}