outcomeOf

inline fun <Ok : Any, Error : Any> outcomeOf(catch: (throwable: Throwable) -> Outcome<Ok, Error> = ::rethrow, block: RaiseScope<Error>.() -> Ok): Outcome<Ok, Error>

Context runner that encapsulates the Ok result of block as an Success, and any raised or caught errors as an Failure.

Note: catch will rethrow by default. This is because the consumer needs to manually override the parameter and map it to an Outcome (if desired). Assigning it to Failure directly will only force Error to be interpreted as Throwable by the RaiseScope, which may interfere with the intended Error type!

// Outcome<Unit, Throwable>
outcomeOf(::Failure) { // this: RaiseScope<Throwable> -> ... }

// Outcome<Int, String>
outcomeOf { // this: RaiseScope<String> ->
raise { "error" }
return 3
}

// Outcome<String, NullPointerException>
outcomeOf { // this: RaiseScope<NullPointerException> ->
catch({ it }) { throw NullPointerException() }
}

Parameters

catch

Map thrown exceptions to an Outcome. (Throws by default).

block

The code to execute.

See also


inline fun <In, Out : Any, Error : Any> In.outcomeOf(catch: (throwable: Throwable) -> Outcome<Out, Error> = ::rethrow, block: RaiseScope<Error>.(In) -> Out): Outcome<Out, Error>

Context runner that encapsulates the result of block as an Success, and any raised or caught errors as an Failure.

Note: catch will rethrow by default. This is because the consumer needs to manually override the parameter and map it to an Outcome (if desired). Assigning it to Failure directly will only force Error to be interpreted as Throwable by the RaiseScope, which may interfere with the intended Error type!

// Outcome<Unit, Throwable>
outcomeOf(::Failure) { // this: RaiseScope<Throwable> -> ... }

// Outcome<Int, String>
3.outcomeOf { // this: RaiseScope<String>, it: Int ->
raise { "error" }
return it
}

// Outcome<String, NullPointerException>
outcomeOf { // this: RaiseScope<NullPointerException> ->
catch({ it }) { throw NullPointerException() }
}

Receiver

Some input type, In, passed to block.

Parameters

Out

The output type of the block, which must be a subtype of Any.

Error

The error type of the Outcome, which must be a subtype of Any.

catch

Map thrown exceptions to an Outcome. (Throws by default).

block

The code to execute.

See also