getOrElse<A> function
- 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 typeA
. This value will be returned if the providedOption
is aNone
.
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);
}