unwrapOr<T> method

S unwrapOr<T>(
  1. T initial
)

Unwraps the Result and returns the Success value. If the result is a Success, returns the success value. If the result is a Failure, returns the provided initial value as a Success.

Use this method when you want to access the Success value directly, but also handle the case when the result is a Failure.

Example usage:

final result = await someAsyncOperation();

final value = result.unwrapOr(0);
print('Success value: $value');

Implementation

S unwrapOr<T>(T initial) {
  if (isSuccess) {
    return _left.value;
  } else {
    return Success(initial as S).value;
  }
}