Result

sealed class Result<Error, Value>

Return type of a computation that could fail.

Result has two subclasses, Success and Failure. Success is used for successful computations. Failure is used for computations that have failed.

Result is dependent on two generic parameters; Error and Value. Error is a type that distinguishes between the kind of failures that could occur. Value is the type of successful computations.

Although Result is a sealed class and can be safely switched over, the idiomatic use is to build up a chain of results with the methods on this class. For an extended example check out this projects wiki.

See also

Parameters

Error

the type that indicates which errors can occur.

Value

the type of a successful computation

Functions

andThen
Link copied to clipboard
fun <T> andThen(chain: (Value) -> Result<Error, T>): Result<Error, T>
Chain two computations togetherIf in a computation chain the result of one computation is needed as input for another computation, andThen is there to chain the computations together.
andThenError
Link copied to clipboard
fun <T> andThenError(chain: (Error) -> Result<T, Value>): Result<T, Value>
Chain two computations together for error recovery.
equals
Link copied to clipboard
open operator fun equals(other: Any?): Boolean
hashCode
Link copied to clipboard
open fun hashCode(): Int
map
Link copied to clipboard
fun <T> map(transform: (Value) -> T): Result<Error, T>
Transform a successful result.
mapError
Link copied to clipboard
fun <T> mapError(transform: (Error) -> T): Result<T, Value>
Transform a failed result.
orThrowException
Link copied to clipboard
fun orThrowException(exceptionProducer: (Error) -> Exception): Value
Unwrap the result by throwing an Exception that can depend on the kind of failureif a computation was a Success, return that data.
toString
Link copied to clipboard
open fun toString(): String
use
Link copied to clipboard
fun use(actOn: (Value) -> Unit): Result<Error, Value>
Use the data of a successful result.
useError
Link copied to clipboard
fun useError(actOn: (Error) -> Unit): Result<Error, Value>
Use the error of a failed result.
withDefault
Link copied to clipboard
fun withDefault(defaultValue: Value): Value
Unwrap the result by providing a default value.
fun withDefault(producer: (Error) -> Value): Value
Unwrap the result by providing a default value that can depend on the kind of failure.

Inheritors

Success
Link copied to clipboard
Failure
Link copied to clipboard