match<A, B, C> function
Matches a TaskEither<A, B> to execute a function based on its Left
or Right
value asynchronously.
Using Dart's pattern matching, the match function provides an expressive and concise way to handle
TaskEither
values without manual type checks and returns a Future<C> representing the computed value.
The returned function uses pattern matching on the result of TaskEither<A, B> and invokes
the relevant asynchronous function (onLeft
for Left
or onRight
for Right
) based on the match.
Example:
final myTask = of<String, int>(10);
final matchFn = match<String, int, String>(
(left) => Future.value("Error: $left"),
(right) => Future.value("Value: $right")
);
final result = await matchFn(myTask);
print(result); // Prints: Value: 10
Implementation
// Future<C> Function(TaskEither<A, B>) match<A, B, C>(
// Future<C> Function(A) onLeft, Future<C> Function(B) onRight) {
// return (TaskEither<A, B> taskEither) async =>
// switch (await taskEither.value()) {
// Left(value: var leftValue) => await onLeft(leftValue),
// Right(value: var rightValue) => await onRight(rightValue)
// };
// }
Future<C> Function(TaskEither<A, B>) match<A, B, C>(
Future<C> Function(A) onLeft, Future<C> Function(B) onRight) {
return matchW<A, B, C, C>(onLeft, onRight);
}