DefaultRaiseScope

Default implementation of RaiseScope.

Tracks scope lifetime with an atomic flag. While active, shortCircuit throws RaisedException. After close is called, it throws RaiseScopeLeakedException instead, indicating the scope has been used beyond its intended lifetime.

Prefer using a builder such as outcomeOf or faultOf rather than constructing a DefaultRaiseScope directly. Construct one manually only when you need to own the scope lifecycle yourself.

Parameters

Error

The type of error that can be raised.

See also

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
inline fun <Ok, Error> RaiseScope<Error>.catch(catch: (throwable: Throwable) -> Error = ::rethrow, block: RaiseScope<Error>.() -> Ok): Ok

Executes block inside the current RaiseScope, converting any thrown Throwable — including fatal kotlin.Error subclasses — to a raised Error via catch.

Link copied to clipboard
inline suspend fun <Ok, Error> RaiseScope<Error>.catching(catch: (throwable: Throwable) -> Error = ::rethrow, block: RaiseScope<Error>.() -> Ok): Ok

Executes block inside the current RaiseScope, converting any thrown Throwable — including fatal kotlin.Error subclasses — to a raised Error via catch.

Link copied to clipboard
open override fun close()

Marks the scope as expired.

Link copied to clipboard
inline fun <Error> RaiseScope<Error>.ensure(condition: Boolean, raise: () -> Error)

Asserts condition is true, otherwise raises the Error produced by raise.

Link copied to clipboard
inline fun <Value, IsType : Any, Error> RaiseScope<Error>.ensureInstanceOf(check: Value, isType: KClass<IsType>, error: (Value) -> Error = { @Suppress(names = ["UNCHECKED_CAST"]) (it as Error) }): IsType

Returns check cast to IsType if it is an instance of that type, otherwise raises the Error produced by error.

Link copied to clipboard
inline fun <Ok, Error> RaiseScope<Error>.ensureNotNull(value: Ok?, raise: () -> Error): Ok & Any

Returns value if it is non-null, otherwise raises the Error produced by raise.

Link copied to clipboard
inline fun <In, Out, Error> RaiseScope<Error>.fold(block: (scope: RaiseScope<Error>) -> In, catch: (Exception) -> Out = ::rethrow, recover: (error: Error) -> Out, transform: (value: In) -> Out): Out

Executes block within this RaiseScope and maps the result using transform, recover, or catch.

Link copied to clipboard
inline suspend fun <In, Out, Error> RaiseScope<Error>.folding(block: suspend (scope: RaiseScope<Error>) -> In, catch: (Exception) -> Out = ::rethrow, recover: suspend (error: Error) -> Out, transform: suspend (value: In) -> Out): Out

Suspend variant of fold that propagates CancellationException for structured concurrency.

Link copied to clipboard

Type-hint helper — tells the compiler the Error type of this RaiseScope is Nothing. Use when type inference fails and no error can ever be raised.

Link copied to clipboard
inline fun <Error> RaiseScope<Error>.raise(error: () -> Error): Nothing

Evaluates error and delegates to shortCircuit, immediately short-circuiting the RaiseScope execution.

Link copied to clipboard

Type-hint helper — tells the compiler the Error type of this RaiseScope. Use when type inference fails and no value is available to infer from.

Type-hint helper — tells the compiler the Error type of this RaiseScope using a concrete type value. Use when type inference fails and a representative value is available.

Link copied to clipboard
open override fun shortCircuit(error: Error): Nothing

Throws the given error, immediately short-circuiting the RaiseScope execution.