matchW<A, B, C, D> function
- D onLeft(
- A
- D onRight(
- B
Matches the given Either value and returns the result of the respective handler.
The matchW
function provides a flexible way to handle Either
types by allowing
the user to specify different return types for the Left
and Right
cases.
This allows for broader use-cases compared to the regular match
.
- The
onLeft
handler is called when the given Either is of typeLeft<A>
. - The
onRight
handler is called when the given Either is of typeRight<B>
.
Usage:
var either = Left<String, int>("Error");
var result = matchW<String, int, String, String>(
(error) => "Failed with $error",
(value) => "Success with $value"
)(either);
print(result); // "Failed with Error"
onLeft
and onRight
handlers can return different types, defined by C
and D
respectively.
Parameters:
onLeft
- The handler function to be called if the Either is of typeLeft<A>
.onRight
- The handler function to be called if the Either is of typeRight<B>
.
Returns:
A function that accepts an Either<A, B> and returns a value of type D
.
Implementation
D Function(Either<A, B>) matchW<A, B, C, D>(
D Function(A) onLeft, D Function(B) onRight) =>
(Either<A, B> either) => switch (either) {
Left(value: var leftValue) => onLeft(leftValue),
Right(value: var rightValue) => onRight(rightValue)
};