of<S extends Object, F extends Exception> static method

Result<S, F> of<S extends Object, F extends Exception>(
  1. Supply<S> throwing
)

Wraps throwing, which may throw an exception, in a Result.

Returns a Success if throwing executes successfully. Otherwise returns a Failure that contains the exception thrown.

Errors are intentionally uncaught since they represent programmatic errors.

Result.of(() => 1)); // Success(1);

Result.of(() => throws FooException())); // Failure(FooException());

Implementation

static Result<S, F> of<S extends Object, F extends Exception>(Supply<S> throwing) {
  try {
    return Success(throwing());

  } on F catch (e) {
    return Failure(e);
  }
}