safely

inline fun <T : Any> safely(crossinline fallback: (Any) -> T, block: RaiseScope<Unit>.() -> T): T

Context runner that uses RaiseScope to safely capture raised or thrown errors, and unwraps either the successful Outcome or the fallback, returning T.

Used in scenarios where you want the advantages of Outcome, but immediately resolve to its Ok type.

Return

The result of the block if successful, or the result of the fallback function if an error occurs.

Parameters

fallback

A function that provides a fallback value in case of an raised or thrown exception.

block

The code to execute within the RaiseScope.


inline fun <In, Out : Any> In.safely(crossinline fallback: (Any) -> Out, block: RaiseScope<Unit>.(In) -> Out): Out

Context runner that uses RaiseScope to safely capture raised or thrown errors, and unwraps either the successful Outcome or the fallback, returning Out.

Used in scenarios where you want the advantages of Outcome, but immediately resolve to its Ok type.

Receiver

Some input type, In, passed to block.

Return

The result of the block if successful, or the result of the fallback function if an error occurs.

Parameters

fallback

A function that provides a fallback value in case of an raised or thrown exception.

block

The code to execute within the RaiseScope.