Package-level declarations

Types

Link copied to clipboard
typealias KotlinResult<T> = Result<T>

Typealias for kotlin.Result, disambiguating it from other result-type classes in the same codebase.

Link copied to clipboard
typealias KotlinResultFlow<Ok> = Flow<KotlinResult<Ok>>

Typealias for a Flow of KotlinResult values.

Properties

Link copied to clipboard

Wraps this value as a KotlinResult failure by converting it to a Throwable first. No exception catching.

Wraps this Throwable directly as a KotlinResult failure. No exception catching.

Link copied to clipboard

Wraps this value as a KotlinResult using result.

Link copied to clipboard

Wraps this value directly as a KotlinResult success. No exception catching.

Link copied to clipboard

Functions

Link copied to clipboard
inline fun <T> KotlinResult<T>.andIf(predicate: (T) -> Boolean, success: (T) -> T): KotlinResult<T>

Conditionally transforms the encapsulated value if success and predicate returns true. If predicate returns false, the value is left unchanged.

Link copied to clipboard
inline fun <I, O> KotlinResult<I>.andThen(success: (I) -> O): KotlinResult<O>

Transforms the encapsulated value if this instance represents success, wrapping the success lambda in result so that any thrown exception is re-encapsulated as a failure rather than propagating.

Link copied to clipboard
inline suspend fun <I, O> KotlinResult<I>.andThenOf(success: (I) -> O): KotlinResult<O>

Transforms the encapsulated value if this instance represents success, wrapping the success lambda in resultOf so that any thrown exception is re-encapsulated as a failure rather than propagating.

inline suspend fun <T> KotlinResult<T>.andThenOf(predicate: (T) -> Boolean, success: (T) -> T): KotlinResult<T>

Conditionally transforms the encapsulated value if success and predicate returns true. If predicate returns false, the value is left unchanged.

Link copied to clipboard
suspend fun <T> Deferred<T>.awaitKotlinResult(): KotlinResult<T>

Awaits the Deferred and returns its result as a KotlinResult.

Link copied to clipboard

Collects a Collection of KotlinResult values into a single result.

Link copied to clipboard
inline fun <T> KotlinResult<T>.exceptionOrDefault(defaultError: Throwable): Throwable

Returns the encapsulated Throwable if failure, or defaultError if success.

Link copied to clipboard
inline fun <T> KotlinResult<T>.exceptionOrElse(onSuccess: (value: T) -> Throwable): Throwable

Returns the encapsulated Throwable if failure, or the result of onSuccess applied to the encapsulated value if success.

Link copied to clipboard

Returns the encapsulated Throwable if this instance represents failure.

Link copied to clipboard

Returns a Flow of unwrapped Throwable values, dropping all successes from the original flow.

Link copied to clipboard

Returns a Flow of unwrapped success values, dropping all failures from the original flow.

Link copied to clipboard
inline fun <In, Out> KotlinResult<In>.flatMap(transform: (In) -> KotlinResult<Out>): KotlinResult<Out>

Applies transform to the encapsulated value if success, returning the KotlinResult produced by transform.

Link copied to clipboard

Flattens a nested KotlinResult.

Link copied to clipboard

Flattens a flow of nested KotlinResult values by applying flatten to each element.

Link copied to clipboard
inline fun <In, Out> KotlinResultFlow<In>.foldResult(crossinline success: suspend (In) -> Out, crossinline failure: suspend (Throwable) -> Out): Flow<Out>

Maps each element of the flow to Out by folding over its success or failure state. Equivalent to calling Result.fold on each element.

Link copied to clipboard
inline fun kotlinFailure(block: () -> Throwable): KotlinResult<Nothing>

Wraps the result of block as a Result.failure.

inline fun kotlinFailure(throwable: Throwable): KotlinResult<Nothing>
Link copied to clipboard
inline fun <T> kotlinSuccess(value: T): KotlinResult<T>

Wraps value as a Result.success.

inline fun <T> kotlinSuccess(block: () -> T): KotlinResult<T>

Wraps the result of block as a Result.success.

Link copied to clipboard
inline fun <In, Out> In.kotlinSuccess(block: In.() -> Out): KotlinResult<Out>

Wraps the result of block as a Result.success.

Link copied to clipboard
inline fun <T> KotlinResult<T>.mapFailure(transform: (Throwable) -> Throwable): KotlinResult<T>

Transforms the encapsulated Throwable if this instance represents failure.

Link copied to clipboard
inline fun <T> KotlinResultFlow<T>.onEachFailure(crossinline action: suspend (Throwable) -> Unit): KotlinResultFlow<T>

Invokes action with the unwrapped Throwable before each failed element is emitted downstream. Successes pass through unaffected.

Link copied to clipboard
inline fun <T> KotlinResultFlow<T>.onEachResult(crossinline success: suspend (T) -> Unit, crossinline failure: suspend (Throwable) -> Unit): KotlinResultFlow<T>

Invokes success or failure before each element is emitted downstream, depending on whether the element is a success or failure.

Link copied to clipboard
inline fun <T> KotlinResultFlow<T>.onEachSuccess(crossinline action: suspend (T) -> Unit): KotlinResultFlow<T>

Invokes action with the unwrapped success value before each successful element is emitted downstream. Failures pass through unaffected.

Link copied to clipboard
inline fun <T> result(finally: () -> Unit = ::unit, block: () -> T): KotlinResult<T>

Non-suspend runner that catches and encapsulates T and all thrown Exceptions as a KotlinResult.

Link copied to clipboard
inline suspend fun <T> resultOf(finally: () -> Unit = ::unit, block: () -> T): KotlinResult<T>

Suspend runner that catches and encapsulates T and most thrown Exceptions as a KotlinResult.

Link copied to clipboard
inline fun <T> T.toKotlinResult(predicate: T.() -> Boolean = { this !is Throwable }, failure: (T) -> Throwable = { value: T -> value.asThrowable { "${it}.asKotlinResult() failed predicate test!" } }): KotlinResult<T>

Wraps this value as a KotlinResult based on predicate.

Link copied to clipboard
inline fun <T> KotlinResult<T>.tryRecover(failure: (Throwable) -> T): KotlinResult<T>

Attempts to recover from a failure by transforming the encapsulated Throwable into a success value, wrapping failure in result.

Link copied to clipboard
inline suspend fun <T> KotlinResult<T>.tryRecoverOf(failure: (Throwable) -> T): KotlinResult<T>

Attempts to recover from a failure by transforming the encapsulated Throwable into a success value, wrapping failure in resultOf.