match<A, B> function
- B onNone(
- B onSome(
- A
A pattern matching function for Option
that provides branching based on the content of the Option
.
This function facilitates pattern matching on an Option<A>
, executing one of the provided functions
based on whether the Option
is a Some
or None
. Both onSome
and onNone
functions have the same
return type in this variation of the match function.
Parameters:
onNone
: A function that gets called if theOption
is aNone
. It returns a value of typeB
.onSome
: A function that gets called if theOption
is aSome
. It accepts a value of typeA
and returns a value of typeB
.
Returns:
- A function that accepts an
Option<A>
and returns a value of typeB
.
Example:
final option = Some(5);
final result = match<int, String>(
() => "It's None",
(value) => "Value is: $value"
)(option);
print(result); // Outputs: Value is: 5
Implementation
B Function(Option<A>) match<A, B>(B Function() onNone, B Function(A) onSome) {
return matchW<A, B, B>(onNone, onSome);
}