Either

sealed class Either<out L, out R>

Represents a value of one of two possible types (a disjoint union). Instances of Either are either an instance of Left or Right. FP Convention dictates that Left is used for "failure" and Right is used for "success".

See also

Inheritors

Types

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

Functions

Link copied to clipboard
inline fun <T, L, R> Either<L, R>.flatMap(fn: (R) -> Either<L, T>): Either<L, T>

Right-biased flatMap() FP convention which means that Right is assumed to be the default case to operate on. If it is Left, operations like map, flatMap, ... return the Left value unchanged.

Link copied to clipboard
inline fun <L, R> Either<L, R>.flatMapLeft(fn: (L) -> Either<L, R>): Either<L, R>

Left-biased flatMap() FP convention which means that Left is assumed to be the default case to operate on. If it is Right, operations like map, flatMap, ... return the Right value unchanged.

Link copied to clipboard
inline fun <L, R, T : Any> Either<L, R>.fold(fnL: (L) -> T, fnR: (R) -> T): T

Applies fnL if this is a Left or fnR if this is a Right.

Link copied to clipboard
inline fun <L, R> Either<L, R>.getOrElse(value: R): R

Returns the value from this Right or the given argument if this is a Left. Right(12).getOrElse(17) RETURNS 12 and Left(12).getOrElse(17) RETURNS 17

inline fun <L, R> Either<L, R>.getOrElse(fn: (L) -> R): R

Returns the value from this Right or the result of fn if this is a Left. Right(12).getOrElse{ it + 3 } RETURNS 12 and Left(12).getOrElse{ it + 3 } RETURNS 15

Link copied to clipboard
inline fun <L, R> Either<L, R>.getOrFail(fn: (failure: L) -> R): R

Right-biased getOrFail() FP convention which means that Right is assumed to be the default case to operate on and return the result. If it is Left, operations like map, flatMap, ... return the Left value unchanged.

Link copied to clipboard
inline fun <L, R> Either<L, R>.getOrNull(): R?

Returns the value from this Right or null if this is a Left. Right(12).getOrNull() RETURNS 12 and Left(12).getOrNull() RETURNS null

Link copied to clipboard
fun <L, R> Either<L, R>.isLeft(): Boolean

Returns true if this is a Left, false otherwise.

Link copied to clipboard
fun <L, R> Either<L, R>.isRight(): Boolean

Returns true if this is a Right, false otherwise.

Link copied to clipboard
fun <L> left(a: L): Either.Left<L>

Creates a Left type.

Link copied to clipboard
inline fun <T, L, R> Either<L, R>.map(fn: (R) -> T): Either<L, T>

Right-biased map() FP convention which means that Right is assumed to be the default case to operate on. If it is Left, operations like map, flatMap, ... return the Left value unchanged.

Link copied to clipboard
inline fun <T, L, R> Either<L, R>.mapLeft(fn: (L) -> T): Either<T, R>

Left-biased map() FP convention which means that Right is assumed to be the default case to operate on. If it is Right, operations like map, flatMap, ... return the Right value unchanged.

Link copied to clipboard
inline fun <L, R, T> Either<L, R>.nullableFold(fnL: (L) -> T?, fnR: (R) -> T?): T?

Applies fnL if this is a Left or fnR if this is a Right.

Link copied to clipboard
inline fun <L, R> Either<L, R>.onFailure(fn: (failure: L) -> Unit): Either<L, R>

Left-biased onFailure() FP convention dictates that when this class is Left, it'll perform the onFailure functionality passed as a parameter, but, overall will still return an Either object, so you chain calls.

Link copied to clipboard
inline fun <L, R> Either<L, R>.onSuccess(fn: (success: R) -> Unit): Either<L, R>

Right-biased onSuccess() FP convention dictates that when this class is Right, it'll perform the onSuccess functionality passed as a parameter, but, overall will still return an Either object, so you chain calls.

Link copied to clipboard
fun <R> right(b: R): Either.Right<R>

Creates a right type.