Result

sealed class Result<out VALUE>

A discriminated union that encapsulates successful outcome either with Success or with Error

This class is adapted to kotlin coroutines so if Error contains CancellationException then this error is rethrown.

Functions

Link copied to clipboard
operator fun component1(): VALUE?
Link copied to clipboard
operator fun component2(): Throwable?
Link copied to clipboard
open override fun toString(): String

Inheritors

Link copied to clipboard
Link copied to clipboard

Extensions

Link copied to clipboard
inline fun <VALUE, NEW_VALUE> Result<VALUE>.fold(onSuccess: (VALUE) -> NEW_VALUE, onError: (Throwable) -> NEW_VALUE): NEW_VALUE

Returns the the result of onSuccess for encapsulated value if this instance represents Success or the result of onError function for encapsulated exception if it is Error.

Link copied to clipboard
inline fun <VALUE> Result<VALUE>.getOrCancel(doBeforeThrow: (Throwable) -> Unit = {}): VALUE

Returns the encapsulated value if this instance represents Success or throws CancellationException with Error.error as its cause if it is Error.

Link copied to clipboard
fun <VALUE> Result<VALUE>.getOrDefault(defaultValue: VALUE): VALUE

Returns the encapsulated value if this instance represents Success or defaultValue for encapsulated exception if it is Error.

Link copied to clipboard
inline fun <VALUE> Result<VALUE>.getOrElse(onError: (Throwable) -> VALUE): VALUE

Returns the encapsulated value if this instance represents Success or result of onError for encapsulated exception if it is Error.

Link copied to clipboard
fun <VALUE> Result<VALUE>.getOrNull(): VALUE?

Returns the encapsulated value if this instance represents Success or null for encapsulated exception if it is Error.

Link copied to clipboard
inline fun <VALUE> Result<VALUE>.getOrThrow(doBeforeThrow: (Throwable) -> Unit = {}): VALUE

Returns the encapsulated value if this instance represents Success or throws the encapsulated exception if it is Error.

Link copied to clipboard
inline fun <VALUE, NEW_VALUE> Result<VALUE>.map(transform: (VALUE) -> NEW_VALUE): Result<NEW_VALUE>

Returns the encapsulated result of the given transform function applied to encapsulated value if this instance represents Success or the original encapsulated exception if it is Error.

Link copied to clipboard
inline fun <VALUE, NEW_VALUE> Result<VALUE>.mapCatching(transform: (VALUE) -> NEW_VALUE): Result<NEW_VALUE>

Returns the encapsulated result of the given transform function applied to encapsulated value if this instance represents Success or the original encapsulated exception if it is Error.

Link copied to clipboard
inline fun <VALUE> Result<VALUE>.recover(transform: (Throwable) -> VALUE): Result<VALUE>

Returns the encapsulated result of the given transform function applied to encapsulated exception if this instance represents Error or the original encapsulated value if it is Success.

Link copied to clipboard
inline fun <VALUE> Result<VALUE>.recoverCatching(transform: (Throwable) -> VALUE): Result<VALUE>

Returns the encapsulated result of the given transform function applied to encapsulated exception if this instance represents Error or the original encapsulated value if it is Success.