Package-level declarations

Types

Link copied to clipboard
Link copied to clipboard
data class Failure<out E : Exception>(val reason: E) : Result<Nothing, E>
Link copied to clipboard
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.

Link copied to clipboard
interface ResultBlock<R, E : Exception>

Used to enforce that certain functions can only be called with the "result" block.

Link copied to clipboard
data class Success<out T>(val value: T) : Result<T, Nothing>

Functions

Link copied to clipboard
infix fun <R, E : Exception, T> T.closeWith(block: (T) -> Unit): T

Provides a handy way to close resources. The specified block of code that is always called after result block has finished, even on failures caused by undefined Throwables or Exceptions.

Link copied to clipboard
inline fun <R, E : Exception> ResultBlock<R, E>.ensure(predicate: Boolean, block: () -> E)

If the predicate evaluates to true, then throw the exception form the body. This should usually be called in a "catching" block.

Link copied to clipboard
inline fun <R, E : Exception> ResultBlock<R, E>.ensureNotNull(predicate: Any?, block: () -> E)

If the predicate is null, then throw the exception form the body. This should usually be called in a "result" block.

Link copied to clipboard
fun <R, E : Exception> ResultBlock<R, E>.failWith(block: () -> E): Nothing

Type safe way of throwing an exception.

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.

infix inline fun <R, E : Exception> Result<R>.failWith(block: (Exception) -> E): R

Return the value of the success case or throws the exception from the "onFailure" block. This acts as a bridge to the standard kotlin.Result.

Link copied to clipboard
fun <R, E : Exception> ResultBlock<R, E>.finally(block: (Result<R, E>) -> Unit)

Defines a block of code that is always called after result block has finished. The block is always executed even on failures caused by undefined Throwables or Exceptions.

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.

Link copied to clipboard
inline fun <R, E : Exception> Any.result(block: ResultBlock<R, E>.() -> R): Result<R, E>

Returns the value produced by the block parameter as a Result. If an exception is thrown within the block that matches the type declared on the Result, then that exception is returned as the result. All other exceptions will be rethrown.