Result

value class Result<out T> : Serializable

A discriminated union that encapsulates a successful outcome with a value of type T or a failure with an arbitrary Throwable exception.

Since Kotlin

1.3

Types

Companion
Link copied to clipboard
object Companion

Companion object for Result class that contains its constructor functions success and failure.

Functions

exceptionOrNull
Link copied to clipboard
fun exceptionOrNull(): Throwable?

Returns the encapsulated Throwable exception if this instance represents failure or null if it is success.

getOrNull
Link copied to clipboard
inline fun getOrNull(): T?

Returns the encapsulated value if this instance represents success or null if it is failure.

toString
Link copied to clipboard
open override fun toString(): String

Returns a string Success(v) if this instance represents success where v is a string representation of the value or a string Failure(x) if it is failure where x is a string representation of the exception.

Properties

isFailure
Link copied to clipboard
val isFailure: Boolean

Returns true if this instance represents a failed outcome. In this case isSuccess returns false.

isSuccess
Link copied to clipboard
val isSuccess: Boolean

Returns true if this instance represents a successful outcome. In this case isFailure returns false.

Extensions

fold
Link copied to clipboard
inline fun <R, T> Result<T>.fold(onSuccess: (T) -> R, onFailure: (exception: Throwable) -> R): R

Returns the result of onSuccess for the encapsulated value if this instance represents success or the result of onFailure function for the encapsulated Throwable exception if it is failure.

getOrDefault
Link copied to clipboard
inline fun <R, T : R> Result<T>.getOrDefault(defaultValue: R): R

Returns the encapsulated value if this instance represents success or the defaultValue if it is failure.

getOrElse
Link copied to clipboard
inline fun <R, T : R> Result<T>.getOrElse(onFailure: (exception: Throwable) -> R): R

Returns the encapsulated value if this instance represents success or the result of onFailure function for the encapsulated Throwable exception if it is failure.

getOrThrow
Link copied to clipboard
inline fun <T> Result<T>.getOrThrow(): T

Returns the encapsulated value if this instance represents success or throws the encapsulated Throwable exception if it is failure.

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

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

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

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

onFailure
Link copied to clipboard
inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit): Result<T>

Performs the given action on the encapsulated Throwable exception if this instance represents failure. Returns the original Result unchanged.

onSuccess
Link copied to clipboard
inline fun <T> Result<T>.onSuccess(action: (T) -> Unit): Result<T>

Performs the given action on the encapsulated value if this instance represents success. Returns the original Result unchanged.

recover
Link copied to clipboard
inline fun <R, T : R> Result<T>.recover(transform: (exception: Throwable) -> R): Result<R>

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

recoverCatching
Link copied to clipboard
inline fun <R, T : R> Result<T>.recoverCatching(transform: (exception: Throwable) -> R): Result<R>

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