commonMain.arrow.OutcomeDsl.kt Maven / Gradle / Ivy
package opensavvy.state.arrow
import arrow.core.raise.Raise
import arrow.core.raise.RaiseDSL
import arrow.core.raise.recover
import opensavvy.state.outcome.Outcome
import kotlin.experimental.ExperimentalTypeInference
import kotlin.jvm.JvmInline
@JvmInline
@RaiseDSL
value class OutcomeDsl(private val raise: Raise) :
Raise by raise {
@RaiseDSL
fun Outcome.bind(): T = when (this) {
is Outcome.Success -> value
is Outcome.Failure -> raise.raise(failure)
}
}
/**
* Arrow-style DSL to execute a [Raise]-based computation to generate an [Outcome].
*/
@OptIn(ExperimentalTypeInference::class)
@RaiseDSL
inline fun out(@BuilderInference block: OutcomeDsl.() -> Value): Outcome =
recover(
block = { Outcome.Success(block(OutcomeDsl(this))) },
recover = { e: Failure -> Outcome.Failure(e) },
)