getOrElse<A, B> function

B Function(Either<A, B>) getOrElse<A, B>(
  1. B defaultFunction(
      )
    )

    Returns a function that, when provided with an Either<A, B>, will yield the value inside if it's a Right, or the result of the defaultFunction if it's a Left.

    The returned function acts as a safe way to extract a value from an Either, providing a fallback mechanism when dealing with Left values.

    Usage:

    final either1 = Right<String, int>(10);
    final either2 = Left<String, int>("Error");
    final fallback = () => 5;
    
    final value1 = getOrElse(fallback)(either1); // returns 10
    final value2 = getOrElse(fallback)(either2); // returns 5
    

    Parameters:

    • defaultFunction: A function that returns a value of type B. This value will be returned if the provided Either is a Left.

    Returns: A function expecting an Either<A, B> and returning a value of type B.

    Implementation

    B Function(Either<A, B>) getOrElse<A, B>(B Function() defaultFunction) {
      return match<A, B, B>(
          (leftValue) => defaultFunction(), (rightValue) => rightValue);
    }