Either

sealed class Either<out A, out B>

Inheritors

Types

Link copied to clipboard
data class Left<out A>(val value: A) : Either<A, Nothing>

The left side of the disjoint union, as opposed to the Right side.

Link copied to clipboard
data class Right<out B>(val value: B) : Either<Nothing, B>

The right side of the disjoint union, as opposed to the Left side.

Functions

Link copied to clipboard
fun <A, B> Either<A, B>.combine(other: Either<A, B>, combineLeft: (A, A) -> A, combineRight: (B, B) -> B): Either<A, B>

Combine two Either values. If both are Right then combine both B values using combineRight or if both are Left then combine both A values using combineLeft, otherwise return the sole Left value (either this or other).

Link copied to clipboard
operator fun <A : Comparable<A>, B : Comparable<B>> Either<A, B>.compareTo(other: Either<A, B>): Int
Link copied to clipboard
inline fun <A, B, C> Either<A, B>.flatMap(f: (right: B) -> Either<A, C>): Either<A, C>

Binds the given function across Right, that is, Map, or transform, the right value B of this Either into a new Either with a right value of type C. Returns a new Either with either the original left value of type A or the newly transformed right value of type C.

Link copied to clipboard
fun <A, B> Either<A, Either<A, B>>.flatten(): Either<A, B>
Link copied to clipboard
inline fun <C> fold(ifLeft: (left: A) -> C, ifRight: (right: B) -> C): C

Transform an Either into a value of C. Alternative to using when to fold an Either into a value C.

Link copied to clipboard
infix inline fun <A, B> Either<A, B>.getOrElse(default: (A) -> B): B

Get the right value B of this Either, or compute a default value with the left value A.

Link copied to clipboard
fun getOrNull(): B?

Returns the unwrapped value B of Either.Right or null if it is Either.Left.

Link copied to clipboard
fun <A, B, C> Either<A, B>.handleErrorWith(f: (A) -> Either<C, B>): Either<C, B>

Binds the given function across Left, that is, Map, or transform, the left value A of this Either into a new Either with a left value of type C. Returns a new Either with either the original right value of type B or the newly transformed left value of type C.

Link copied to clipboard

Returns true if this is a Left, false otherwise.

inline fun isLeft(predicate: (A) -> Boolean): Boolean

Returns false if Right or returns the result of the given predicate to the Left value.

Link copied to clipboard

Returns true if this is a Right, false otherwise.

inline fun isRight(predicate: (B) -> Boolean): Boolean

Returns false if Left or returns the result of the given predicate to the Right value.

Link copied to clipboard
fun leftOrNull(): A?

Returns the unwrapped value A of Either.Left or null if it is Either.Right.

Link copied to clipboard
inline fun <C> map(f: (right: B) -> C): Either<A, C>

Map, or transform, the right value B of this Either to a new value C.

Link copied to clipboard
inline fun <C> mapLeft(f: (A) -> C): Either<C, B>

Map, or transform, the left value A of this Either to a new value C.

Link copied to clipboard
fun <A> Either<A, A>.merge(): A

Returns the value from this Right or Left.

Link copied to clipboard
inline fun onLeft(action: (left: A) -> Unit): Either<A, B>

Performs the given action on the encapsulated A if this instance represents Either.Left. Returns the original Either unchanged.

Link copied to clipboard
inline fun onRight(action: (right: B) -> Unit): Either<A, B>

Performs the given action on the encapsulated B value if this instance represents Either.Right. Returns the original Either unchanged.

Link copied to clipboard
fun swap(): Either<B, A>

Swap the generic parameters A and B of this Either.

Link copied to clipboard
fun <E, A> Either<E, A>.toEitherNel(): EitherNel<E, A>
Link copied to clipboard
open override fun toString(): String