getOrElse<A> function

A Function(Option<A>) getOrElse<A>(
  1. A defaultFunction()
)

Returns a function that, when provided with an Option<A>, will yield the value inside if it's a Some, or the result of the defaultFunction if it's a None.

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

Usage:

final option1 = Some(10);
final option2 = None<int>();
final fallback = () => 5;

final value1 = getOrElse(fallback)(option1); // returns 10
final value2 = getOrElse(fallback)(option2); // returns 5

Parameters:

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

Returns: A function expecting an Option<A> and returning a value of type A.

Implementation

A Function(Option<A>) getOrElse<A>(A Function() defaultFunction) {
  return match<A, A>(() => defaultFunction(), (someValue) => someValue);
}