Result

value class Result<out T> : Serializable(source)

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

Link copied to clipboard
object Companion

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

Since Kotlin 1.3

Properties

Link copied to clipboard

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

Since Kotlin 1.3
Link copied to clipboard

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

Since Kotlin 1.3

Functions

Link copied to clipboard

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

Since Kotlin 1.3
Link copied to clipboard
inline fun <R, T> Result<T>.fold(onSuccess: (value: 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.

Since Kotlin 1.3
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.

Since Kotlin 1.3
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.

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

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

Since Kotlin 1.3
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.

Since Kotlin 1.3
Link copied to clipboard
inline fun <R, T> Result<T>.map(transform: (value: 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.

Since Kotlin 1.3
Link copied to clipboard
inline fun <R, T> Result<T>.mapCatching(transform: (value: 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.

Since Kotlin 1.3
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.

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

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

Since Kotlin 1.3
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.

Since Kotlin 1.3
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.

Since Kotlin 1.3
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.

Since Kotlin 1.3