matchW<A, B, C> function

C Function(Option<A>) matchW<A, B, C>(
  1. C onNone(),
  2. C onSome(
    1. 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 the Option is a None. It returns a value of type C.
  • onSome: A function that gets called if the Option is a Some. It accepts a value of type A and returns a value of type C.

Returns:

  • A function that accepts an Option<A> and returns a value of type C.

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) };