match<A, B, C> function
- C onLeft(
- A
- C onRight(
- B
Matches an Either<A, B> to execute a function based on its Left or Right value.
Using Dart's pattern matching, the match function provides an expressive
and concise way to handle Either
values without manual type checks.
The returned function uses pattern matching on Either<A, B> and invokes
the relevant function (onLeft
for Left
or onRight
for Right
) based on the match.
Example:
void main() {
final right = Right(5);
final left = Left("Error");
final matchFn = match(
(error) => "It's Left with value: $error",
(value) => "It's Right with value: $value"
);
print(matchFn(right)); // Prints: It's Right with value: 5
print(matchFn(left)); // Prints: It's Left with value: Error
}
Implementation
C Function(Either<A, B>) match<A, B, C>(
C Function(A) onLeft, C Function(B) onRight) {
return matchW<A, B, C, C>(onLeft, onRight);
}