matchW<A, B, C, D> function

Future<D> Function(TaskEither<A, B>) matchW<A, B, C, D>(
  1. Future<D> onLeft(
    1. A
    ),
  2. Future<D> onRight(
    1. B
    )
)

Applies the provided functions onLeft and onRight to handle the contents of a TaskEither instance. The returned function takes a TaskEither as input, awaits its computation, and depending on whether it's a Left or Right, invokes the appropriate function.

Example:

final taskEither = TaskEither<int, String>(() => Future.value(Right<String, int>(5)));

final result = await matchW<int, String, int, String>(
    (leftValue) async => 'Error: $leftValue',
    (rightValue) async => 'Success: $rightValue')(taskEither);

print(result); // Output: Success: 5

Implementation

Future<D> Function(TaskEither<A, B>) matchW<A, B, C, D>(
    Future<D> Function(A) onLeft, Future<D> Function(B) onRight) {
  return (TaskEither<A, B> taskEither) async =>
      switch (await taskEither.value()) {
        e.Left(value: var leftValue) => await onLeft(leftValue),
        e.Right(value: var rightValue) => await onRight(rightValue)
      };
}