Result

sealed class Result<out T, out E : Exception>

A Result class that enables the type of error case to be defined. In kotlin, exceptions are meant to be returned as opposed to being thrown. The Result class helps to simplify that approach.

(https://elizarov.medium.com/kotlin-and-exceptions-8062f589d07)

Inheritors

Functions

Link copied to clipboard
infix inline fun <R, E : Exception, R2, E2 : Exception> Result<R2, E2>.failWith(block: (E2) -> E): R2

Return the value of the success case or fail with the exception returned from the "block" block.

Link copied to clipboard
fun <R, E : Exception> Result<R, E>.getOrNull(): R?

Return the value of the success case or null.

Link copied to clipboard
fun <R, E : Exception> Result<R, E>.getOrThrow(): R

Return the value of the success case or throws the exception.

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

Creates a new Result with the value mapped to a new type.

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

Creates a new Result with the exception mapped to a new type.

Link copied to clipboard
infix inline fun <R, E : Exception> Result<R, E>.onFailure(block: (E) -> Unit): Result<R, E>

Calls block if the result is a Failure.

Link copied to clipboard
inline fun <R, E : Exception> Result<R, E>.recover(transform: (E) -> R?): Result<R, E>

Provides a way to recover from a failure. The transform block is only called if the result is a Failure.