binding

inline fun <V, E> binding(crossinline block: ResultBinding<E>.() -> V): Result<V, E>

Calls the specified function block with ResultBinding as its receiver and returns its Result.

When inside a binding block, the bind function is accessible on any Result. Calling the bind function will attempt to unwrap the Result and locally return its value. If the Result is an Err, the binding block will terminate with that bind and return that failed-to-bind Err.

Example:

fun provideX(): Result<Int, ExampleErr> { ... }
fun provideY(): Result<Int, ExampleErr> { ... }

val result: Result<Int, ExampleErr> = binding {
val x = provideX().bind()
val y = provideY().bind()
x + y
}