Result

Common
JVM
JS
Native
1.3
inline class Result<out T> : Serializable

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

Properties

Common
JVM
JS
Native
1.0

isFailure

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

val isFailure: Boolean
Common
JVM
JS
Native
1.0

isSuccess

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

val isSuccess: Boolean

Functions

Common
JVM
JS
Native
1.0

exceptionOrNull

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

fun exceptionOrNull(): Throwable?
Common
JVM
JS
Native
1.0

getOrNull

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

fun getOrNull(): T?
Common
JVM
JS
Native
1.0

toString

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.

fun toString(): String

Companion Object Functions

Common
JVM
JS
Native
1.0

failure

Returns an instance that encapsulates the given exception as failure.

fun <T> failure(exception: Throwable): Result<T>
Common
JVM
JS
Native
1.0

success

Returns an instance that encapsulates the given value as successful value.

fun <T> success(value: T): Result<T>

Extension Functions

Common
JVM
JS
Native
1.3

fold

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

fun <R, T> Result<T>.fold(
    onSuccess: (value: T) -> R,
    onFailure: (exception: Throwable) -> R
): R
Common
JVM
JS
Native
1.3

getOrDefault

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

fun <R, T : R> Result<T>.getOrDefault(defaultValue: R): R
Common
JVM
JS
Native
1.3

getOrElse

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

fun <R, T : R> Result<T>.getOrElse(
    onFailure: (exception: Throwable) -> R
): R
Common
JVM
JS
Native
1.3

getOrThrow

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

fun <T> Result<T>.getOrThrow(): T
Common
JVM
JS
Native
1.3

map

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 failure.

fun <R, T> Result<T>.map(
    transform: (value: T) -> R
): Result<R>
Common
JVM
JS
Native
1.3

mapCatching

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 failure.

fun <R, T> Result<T>.mapCatching(
    transform: (value: T) -> R
): Result<R>
Common
JVM
JS
Native
1.3

onFailure

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

fun <T> Result<T>.onFailure(
    action: (exception: Throwable) -> Unit
): Result<T>
Common
JVM
JS
Native
1.3

onSuccess

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

fun <T> Result<T>.onSuccess(
    action: (value: T) -> Unit
): Result<T>
Common
JVM
JS
Native
1.3

recover

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

fun <R, T : R> Result<T>.recover(
    transform: (exception: Throwable) -> R
): Result<R>
Common
JVM
JS
Native
1.3

recoverCatching

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

fun <R, T : R> Result<T>.recoverCatching(
    transform: (exception: Throwable) -> R
): Result<R>