throwfold

inline fun <In, Out> In.throwfold(throws: (Throwable) -> Out, pass: (In) -> Out): Out

Syntax-sugar for a lambda folds a potentially Throwable receiver type into type Out.

This function does not catch exceptions.

val result: Throwable = IllegalStateException()

// Before:
(result as? Throwable)?.let { "$it" } ?: "The world is good!"

// After:
result.throwfold(throws = { "$it" }, pass = { "The world is good!" })

Return

The result of the lambda passed to throws or pass.

Parameters

In

The type of the receiver.

Out

The type of the return value.

throws

The lambda to call if the receiver is a Throwable.

pass

The lambda to call if the receiver is not a Throwable.