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

component1
Link copied to clipboard
operator fun component1(): VALUE?
component2
Link copied to clipboard
operator fun component2(): Throwable?
equals
Link copied to clipboard
open operator fun equals(other: Any?): Boolean
hashCode
Link copied to clipboard
open fun hashCode(): Int
toString
Link copied to clipboard
open override fun toString(): String

Inheritors

Success
Link copied to clipboard
Error
Link copied to clipboard

Extensions

fold
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.
getOrCancel
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.
getOrDefault
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.
getOrElse
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.
getOrNull
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.
getOrThrow
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.
map
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.
mapCatching
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.
recover
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.
recoverCatching
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.