andThenOf

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.

This is the outcomeOf alternative to mapSuccess — it catches exceptions rather than letting them propagate.

outcomeOf { 4 }
.andThenOf { it * 2 } // Success(8)
.andThenOf { check(false) { "$it" } } // Failure(IllegalStateException("8"))
.andThenOf { 16 } // Remains Failure(IllegalStateException("8"))

Parameters

In

The input Success value type.

Out

The output Success value type after transformation.

Error

The Failure error type.

catch

Handles exceptions thrown by success. Defaults to rethrowing via rethrow.

scope

The RaiseScope used to raise typed errors.

success

The suspend transform applied to the Success value.

See also


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.

outcomeOf { 4 }.andThenOf({ it > 0 }) { it * 2 } // Success(8)
outcomeOf { 4 }.andThenOf({ it < 0 }) { it * 2 } // Success(4) — predicate false, unchanged

Parameters

Ok

The Success value type.

Error

The Failure error type.

predicate

Guards the transformation. If false, success is skipped.

catch

Handles exceptions thrown by success. Defaults to rethrowing via rethrow.

scope

The RaiseScope used to raise typed errors.

success

The transform applied when predicate is true.

See also