Package-level declarations

Types

Link copied to clipboard
typealias Fault<Error> = Outcome<Unit, Error>

Alias for Outcome used when only the error is useful — the success is irrelevant.

Link copied to clipboard
typealias FaultFlow<Error> = Flow<Fault<Error>>

Alias for a Flow of Fault values.

Link copied to clipboard
typealias Maybe<Ok> = Outcome<Ok, Unit>

Alias for Outcome used when only the success value is useful — the error is irrelevant.

Link copied to clipboard
typealias MaybeFlow<Ok> = Flow<Maybe<Ok>>

Alias for a Flow of Maybe values.

Link copied to clipboard
typealias OutcomeFlow<Ok, Error> = Flow<Outcome<Ok, Error>>

Alias for a Flow of Outcome values.

Properties

Link copied to clipboard

Directly wraps any T as a Failure. No type inference or null-checks performed.

Link copied to clipboard

Converts any Outcome to a Fault by discarding the Success value (mapping it to Unit).

Link copied to clipboard
val <Ok> Outcome<Ok, *>.asMaybe: Maybe<Ok>

Converts any Outcome to a Maybe by discarding the Failure error (mapping it to Unit).

Link copied to clipboard

Converts a KotlinResult to an Outcome with Throwable as the error type.

Link copied to clipboard

Directly wraps any T as a Success. No type inference or null-checks performed.

Link copied to clipboard
@get:JvmName(name = "flattenNestedFailureAlias")
val <Ok, Err, Oki : Ok, Oko : Ok> Outcome<Oko, Outcome<Oki, Err>>.flatten: Outcome<Ok, Err>

Property alias for flattenNestedFailure.

@get:JvmName(name = "flattenNestedBothAlias")
val <Ok, Err, Oks : Ok, Ers : Err, Okf : Ok, Erf : Err> Outcome<Outcome<Oks, Ers>, Outcome<Okf, Erf>>.flatten: Outcome<Ok, Err>

Property alias for flattenNestedBoth.

@get:JvmName(name = "flattenNestedSuccessAlias")
val <Ok, Err, Eri : Err, Ero : Err> Outcome<Outcome<Ok, Eri>, Ero>.flatten: Outcome<Ok, Err>

Property alias for flattenNestedSuccess.

Link copied to clipboard

Smart-wraps any T as a Fault.

Link copied to clipboard
val <T> T.toMaybe: Maybe<T & Any>

Smart-wraps any T as a Maybe.

Link copied to clipboard

Smart-wraps any T as an Outcome.

Functions

Link copied to clipboard

Combines every Outcome in the Iterable into a single result.

Link copied to clipboard
inline fun <In, Out, Error> Outcome<In, Error>.andThen(catch: (throwable: Throwable) -> Outcome<Out, Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), success: RaiseScope<Error>.(In) -> Out): Outcome<Out, Error>

Transforms the Success value of this Outcome, wrapping the result with outcome error-catching semantics.

inline fun <Ok, Error> Outcome<Ok, Error>.andThen(predicate: (Ok) -> Boolean, catch: (throwable: Throwable) -> Outcome<Ok, Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), success: RaiseScope<Error>.(Ok) -> Ok): Outcome<Ok, Error>

Conditionally transforms the Success value, but only when predicate returns true. If predicate returns false, the Success is returned unchanged.

Link copied to clipboard
inline suspend fun <In, Out, Error> Outcome<In, Error>.andThenOf(catch: (throwable: Throwable) -> Outcome<Out, Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), success: suspend RaiseScope<Error>.(In) -> Out): Outcome<Out, Error>

Transforms the Success value of this Outcome, wrapping the result with outcomeOf error-catching semantics.

inline suspend fun <Ok, Error> Outcome<Ok, Error>.andThenOf(predicate: (Ok) -> Boolean, catch: (throwable: Throwable) -> Outcome<Ok, Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), success: RaiseScope<Error>.(Ok) -> Ok): Outcome<Ok, Error>

Conditionally transforms the Success value, but only when predicate returns true. If predicate returns false, the Success is returned unchanged.

Link copied to clipboard
suspend fun <T : Any> Deferred<T>.awaitFault(): Fault<Throwable>

Awaits the result of a Deferred and wraps it as a Fault.

Link copied to clipboard
suspend fun <T : Any> Deferred<T>.awaitMaybe(): Maybe<T>

Awaits the result of a Deferred and wraps it as a Maybe.

Link copied to clipboard
suspend fun <T : Any> Deferred<T>.awaitOutcome(): Outcome<T, Throwable>

Awaits the result of a Deferred and wraps it as an Outcome.

Link copied to clipboard
inline fun <Ok> catchException(scope: RaiseScope<Exception> = DefaultRaiseScope(), block: RaiseScope<Exception>.() -> Ok): Outcome<Ok, Exception>

Runs block and wraps the result as an Outcome, using Exception as the error type.

Link copied to clipboard
inline suspend fun <Ok> catchExceptionOf(scope: RaiseScope<Exception> = DefaultRaiseScope(), block: suspend RaiseScope<Exception>.() -> Ok): Outcome<Ok, Exception>

Suspend variant of catchException builder.

Link copied to clipboard
inline fun <Ok> catchString(scope: RaiseScope<String> = DefaultRaiseScope(), block: RaiseScope<String>.() -> Ok): Outcome<Ok, String>

Runs block and wraps the result as an Outcome, using String as the error type.

Link copied to clipboard
inline suspend fun <Ok> catchStringOf(scope: RaiseScope<String> = DefaultRaiseScope(), block: suspend RaiseScope<String>.() -> Ok): Outcome<Ok, String>

Suspend variant of catchString builder.

Link copied to clipboard
infix inline fun <Ok, Error> Outcome<Ok, Error>.coerceToFailure(falter: (Ok) -> Error): Failure<Error>

Coerces this Outcome to a Failure.

Link copied to clipboard
infix inline fun <Ok, Error> Outcome<Ok, Error>.coerceToSuccess(recover: (Error) -> Ok): Success<Ok>

Coerces this Outcome to a Success.

Link copied to clipboard

Extracts either the Success value or the Failure error, returning the nearest common Ancestor type.

Link copied to clipboard

Returns a Flow that collapses each Outcome to its nearest common Ancestor type.

Link copied to clipboard
inline fun emptyFailure(ignore: Any? = null): Failure<Unit>

Returns a Failure whose error is Unit. Convenient when a Maybe must signal absence without providing an error value.

Link copied to clipboard
inline fun emptySuccess(ignore: Any? = null): Success<Unit>

Returns a Success whose value is Unit. Convenient when a Fault must signal success without providing a value.

Link copied to clipboard
infix fun <Ok, Error> Outcome<Ok, Error>.errorOrDefault(default: Error): Error

Returns the Failure error, or default if the outcome is a Success.

Link copied to clipboard
infix inline fun <Ok, Error> Outcome<Ok, Error>.errorOrElse(faulter: (Ok) -> Error): Error

Returns the Failure error, or the result of faulter applied to the Success value.

Link copied to clipboard

Returns the Failure error, or null if the outcome is a Success.

Link copied to clipboard

Returns the Failure error, or short-circuits the surrounding RaiseScope with the Success value.

Link copied to clipboard

Returns the Failure error, or throws if the outcome is a Success.

Link copied to clipboard
inline fun <Error> failure(block: () -> Error): Failure<Error>

Builds a Failure whose error is the value returned by block.

Link copied to clipboard
infix inline fun <Ok, Error> Outcome<Ok, Error>.falter(transform: (Ok) -> Error): Failure<Error>

Infix alias for coerceToFailure.

Link copied to clipboard
inline fun <Error> fault(catch: (Exception) -> Fault<Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), block: RaiseScope<Error>.() -> Unit): Fault<Error>

Runs block inside a RaiseScope and returns a Fault.

Link copied to clipboard
inline suspend fun <Error> faultOf(catch: (Exception) -> Fault<Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), block: suspend RaiseScope<Error>.() -> Unit): Fault<Error>

Suspend variant of fault builder.

Link copied to clipboard

Returns a Flow containing only the error of each Failure outcome. Success outcomes are filtered out.

Link copied to clipboard

Returns a Flow containing only the value of each Success outcome. Failure outcomes are filtered out.

Link copied to clipboard

Transforms Outcome<Ok, ErrorIn> into Outcome<Ok, ErrorOut> by applying transform to the Failure error.

Link copied to clipboard
infix inline fun <In, Out, Error> Outcome<In, Error>.flatMapSuccess(transform: (In) -> Outcome<Out, Error>): Outcome<Out, Error>

Transforms Outcome<In, Error> into Outcome<Out, Error> by applying transform to the Success value.

Link copied to clipboard
Link copied to clipboard
inline fun <Ok, Error, Output> Outcome<Ok, Error>.fold(failure: Failure<Error>.() -> Output, success: Success<Ok>.() -> Output): Output

Applies success or failure to the receiver Outcome, returning Output.

Link copied to clipboard
inline fun <Ok, Error, Output> OutcomeFlow<Ok, Error>.foldOutcome(crossinline success: suspend (Ok) -> Output, crossinline failure: suspend (Error) -> Output): Flow<Output>

Returns a Flow of Output by applying success or failure to each Outcome in the upstream flow.

Link copied to clipboard
infix fun <Ok, Error> Outcome<Ok, Error>.getOrDefault(default: Ok): Ok

Returns the Success value, or default if the outcome is a Failure.

Link copied to clipboard
infix inline fun <Ok, Error> Outcome<Ok, Error>.getOrElse(recover: (Error) -> Ok): Ok

Returns the Success value, or the result of recover applied to the Failure error.

Link copied to clipboard

Returns the Success value, or null if the outcome is a Failure.

Link copied to clipboard

Returns the Success value, or short-circuits the surrounding RaiseScope with the Failure error.

Link copied to clipboard

Returns the Success value, or throws if the outcome is a Failure.

Link copied to clipboard

Swaps the Success value and the Failure error, producing Outcome<Error, Ok>.

Link copied to clipboard

Returns true if this Outcome is a Failure.

infix inline fun <Ok, Error> Outcome<Ok, Error>.isFailure(predicate: (Error) -> Boolean): Boolean

Returns true if this is a Failure and predicate returns true for the error.

Link copied to clipboard

Returns true if this Outcome is a Success.

infix inline fun <Ok, Error> Outcome<Ok, Error>.isSuccess(predicate: (Ok) -> Boolean): Boolean

Returns true if this is a Success and predicate returns true for the value.

Link copied to clipboard
inline fun <In, Out, ErrorIn, ErrorOut> Outcome<In, ErrorIn>.map(failure: (ErrorIn) -> ErrorOut, success: (In) -> Out): Outcome<Out, ErrorOut>

Transforms both states of an Outcome, re-wrapping each result in the same kind of Outcome.

Link copied to clipboard
inline fun <Ok, ErrorIn, ErrorOut> OutcomeFlow<Ok, ErrorIn>.mapFailure(crossinline transform: suspend (ErrorIn) -> ErrorOut): OutcomeFlow<Ok, ErrorOut>

Returns a new OutcomeFlow with each Failure error transformed by transform. Success outcomes pass through unaffected.

infix inline fun <Ok, ErrorIn, ErrorOut> Outcome<Ok, ErrorIn>.mapFailure(transform: (ErrorIn) -> ErrorOut): Outcome<Ok, ErrorOut>

Transforms Outcome<Ok, ErrorIn> into Outcome<Ok, ErrorOut> by applying transform to the Failure error.

Link copied to clipboard
inline fun <In, Out, Error> OutcomeFlow<In, Error>.mapSuccess(crossinline transform: suspend (In) -> Out): OutcomeFlow<Out, Error>

Returns a new OutcomeFlow with each Success value transformed by transform. Failure outcomes pass through unaffected.

infix inline fun <In, Out, Error> Outcome<In, Error>.mapSuccess(transform: (In) -> Out): Outcome<Out, Error>

Transforms Outcome<In, Error> into Outcome<Out, Error> by applying transform to the Success value.

Link copied to clipboard
inline fun <Ok> maybe(catch: (Exception) -> Maybe<Ok> = ::emptyFailure, scope: RaiseScope<Any?> = DefaultRaiseScope(), block: RaiseScope<Any?>.() -> Ok): Maybe<Ok>

Runs block inside a RaiseScope and returns a Maybe.

Link copied to clipboard
inline suspend fun <Ok> maybeOf(catch: (Exception) -> Maybe<Ok> = ::emptyFailure, scope: RaiseScope<Any?> = DefaultRaiseScope(), block: suspend RaiseScope<Any?>.() -> Ok): Maybe<Ok>

Suspend variant of maybe builder.

Link copied to clipboard
inline fun <Ok, Error> OutcomeFlow<Ok, Error>.onEachFailure(crossinline block: suspend (Error) -> Unit): OutcomeFlow<Ok, Error>

Returns an OutcomeFlow that invokes block for each Failure outcome before emitting it downstream. Success outcomes pass through unaffected.

Link copied to clipboard
inline fun <Ok, Error> OutcomeFlow<Ok, Error>.onEachSuccess(crossinline block: suspend (Ok) -> Unit): OutcomeFlow<Ok, Error>

Returns an OutcomeFlow that invokes block for each Success outcome before emitting it downstream. Failure outcomes pass through unaffected.

Link copied to clipboard
infix inline fun <Ok, Error> Outcome<Ok, Error>.onFailure(block: (Error) -> Unit): Outcome<Ok, Error>

Calls block with the Failure error if this is a failure, then returns the original Outcome unchanged. No-op on Success.

Link copied to clipboard
infix inline fun <Ok, Error> Outcome<Ok, Error>.onSuccess(block: (Ok) -> Unit): Outcome<Ok, Error>

Calls block with the Success value if this is a success, then returns the original Outcome unchanged. No-op on Failure.

Link copied to clipboard
inline fun <Ok, Error> outcome(catch: (Exception) -> Outcome<Ok, Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), block: RaiseScope<Error>.() -> Ok): Outcome<Ok, Error>

Runs block inside a RaiseScope and returns an Outcome.

Link copied to clipboard
inline suspend fun <Ok, Error> outcomeOf(catch: (Exception) -> Outcome<Ok, Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), block: suspend RaiseScope<Error>.() -> Ok): Outcome<Ok, Error>

Suspend variant of outcome builder.

Link copied to clipboard
infix inline fun <Ok, Error> Outcome<Ok, Error>.recover(transform: (Error) -> Ok): Success<Ok>

Infix alias for coerceToSuccess.

Link copied to clipboard
inline fun <Ok, Error, Output> Outcome<Ok, Error>.rfold(success: Success<Ok>.() -> Output, failure: Failure<Error>.() -> Output): Output

Reverse fold — identical behaviour with swapped lambda argument order.

Link copied to clipboard
inline fun <Ok> success(block: () -> Ok): Success<Ok>

Builds a Success whose value is the value returned by block.

Link copied to clipboard
inline fun <Ok, Error> Outcome<Ok, Error>.throwIf(fallbackMessage: (Error) -> String = { "Outcome was Failure and throwIf predicate was true: $it" }, predicate: (Error) -> Boolean): Outcome<Ok, Error>

Throws if this is a Failure and predicate returns true for the error. Returns the original Outcome unchanged otherwise.

Link copied to clipboard
inline fun <Ok, Error> Outcome<Ok, Error>.throwUnless(fallbackMessage: (Error) -> String = { "Outcome was Failure and throwUnless predicate was false: $it" }, predicate: (Error) -> Boolean): Outcome<Ok, Error>

Throws if this is a Failure and predicate returns false for the error. Returns the original Outcome unchanged otherwise.

Link copied to clipboard
inline fun <T> T.toFault(isFailure: T.() -> Boolean): Fault<T>

Wraps this value as a Fault based on isFailure.

Link copied to clipboard
inline fun <Ok> Ok.toMaybe(isSuccess: Ok.() -> Boolean): Maybe<Ok>

Wraps this value as a Maybe based on isSuccess.

Link copied to clipboard
fun <Ok, Error> Ok.toOutcome(predicate: Ok.() -> Boolean, faulter: Ok.() -> Error): Outcome<Ok, Error>

Wraps this value as an Outcome based on a predicate.

Link copied to clipboard
inline fun <Ok, ErrorIn, ErrorOut> Outcome<Ok, ErrorIn>.tryRecover(catch: (throwable: Throwable) -> Outcome<Ok, ErrorOut> = ::rethrow, scope: RaiseScope<ErrorOut> = DefaultRaiseScope(), failure: RaiseScope<ErrorOut>.(ErrorIn) -> Ok): Outcome<Ok, ErrorOut>

Transforms a Failure into a Success, wrapping the result with outcome error-catching semantics.

Link copied to clipboard
inline suspend fun <Ok, ErrorIn, ErrorOut> Outcome<Ok, ErrorIn>.tryRecoverOf(catch: (throwable: Throwable) -> Outcome<Ok, ErrorOut> = ::rethrow, scope: RaiseScope<ErrorOut> = DefaultRaiseScope(), failure: RaiseScope<ErrorOut>.(ErrorIn) -> Ok): Outcome<Ok, ErrorOut>

Transforms a Failure into a Success, wrapping the result with outcomeOf error-catching semantics.