swap<A, B> function
- Either<
A, B> either
Swaps the inner types of an Either
value.
If the provided Either
is a Left
, the resulting value will be a Right
with the same inner value, and vice versa.
Example:
final leftValue = Left<String, int>('error');
final swappedToLeft = swap(leftValue);
print(swappedToLeft); // Outputs: Right('error')
final rightValue = Right<String, int>(42);
final swappedToRight = swap(rightValue);
print(swappedToRight); // Outputs: Left(42)
either
: The Either
value to be swapped.
Returns: A new Either
value with swapped inner types.
Implementation
Either<B, A> swap<A, B>(Either<A, B> either) {
switch (either) {
case Left(value: var leftValue):
return Right<B, A>(leftValue);
case Right(value: var rightValue):
return Left<B, A>(rightValue);
}
}