when<T> method
T
when<T>(
- T left(
- L
- T right(
- R
Returns the result of applying either left or right function depending
on whether the value is of type Left or Right.
If the value is of type Left, the left function is applied to the
value of type L. Otherwise, the right function is applied to the
value of type R.
Example usage:
final value = Right<int, String>('hello');
final result = value.when(
(leftValue) => leftValue * 2, // not executed
(rightValue) => rightValue.toUpperCase(), // executed and returns 'HELLO'
);
Implementation
T when<T>(
T Function(L) left,
T Function(R) right,
) {
if (this is Left<L, R>) {
return left((this as Left<L, R>).value);
}
return right((this as Right<L, R>).value);
}