matchW<A, B, C> function
- C onNone(
- C onSome(
- A
A more generalized match function for Option that returns a value of a potentially different type.
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. It is more flexible than the standard match function
by allowing the return types of onSome and onNone to differ.
Parameters:
onNone: A function that gets called if theOptionis aNone. It returns a value of typeC.onSome: A function that gets called if theOptionis aSome. It accepts a value of typeAand returns a value of typeC.
Returns:
- A function that accepts an
Option<A>and returns a value of typeC.
Example:
final option = Some(5);
final result = matchW<int, String, String>(
() => "It's None",
(value) => "Value is: $value"
)(option);
print(result); // Outputs: Value is: 5
Implementation
C Function(Option<A>) matchW<A, B, C>(
C Function() onNone, C Function(A) onSome) =>
(Option<A> option) =>
switch (option) { None() => onNone(), Some(value: var v) => onSome(v) };