maybeOf

inline suspend fun <Ok> maybeOf(catch: (Exception) -> Maybe<Ok> = ::emptyFailure, scope: RaiseScope<Any?> = DefaultRaiseScope(), block: suspend RaiseScope<Any?>.() -> Ok): Maybe<Ok>

Suspend variant of maybe builder.

Runs block inside a RaiseScope and returns a Maybe. kotlinx.coroutines.CancellationException is correctly propagated for structured concurrency.

The error type is always mapped to Unit; use raise to short-circuit with an absent value.

// Maybe<String> — success
val m: Maybe<String> = maybeOf { "hello".uppercase() }

// Maybe<Int> — absent
val n: Maybe<Int> = maybeOf { raise { Unit } }

Parameters

catch

Maps a thrown Exception to a Maybe. Returns emptyFailure by default.

block

The suspend block to execute within the scope.

See also