ensureNotNull

suspend fun <R, B : Any> EagerEffectScope<R>.ensureNotNull(value: B?, shift: () -> R): B(source)

Ensure that value is not null. if it's non-null it will be smart-casted and returned if it's false it will shift with the provided value R. Monadic version of kotlin.requireNotNull.

import arrow.core.continuations.eagerEffect
import arrow.core.continuations.ensureNotNull
import arrow.core.left
import arrow.core.right
import io.kotest.matchers.shouldBe

fun main() {
val failure = "failed"
val int: Int? = null
eagerEffect<String, Int> {
ensureNotNull(int) { failure }
}.toEither() shouldBe (int?.right() ?: failure.left())
}

suspend fun <R, B : Any> EffectScope<R>.ensureNotNull(value: B?, shift: () -> R): B(source)

Ensure that value is not null. if it's non-null it will be smart-casted and returned if it's false it will shift with the provided value R. Monadic version of kotlin.requireNotNull.

import arrow.core.continuations.effect
import arrow.core.continuations.ensureNotNull
import arrow.core.left
import arrow.core.right
import io.kotest.matchers.shouldBe

suspend fun main() {
val failure = "failed"
val int: Int? = null
effect<String, Int> {
ensureNotNull(int) { failure }
}.toEither() shouldBe (int?.right() ?: failure.left())
}

suspend fun <B> NullableEffectScope.ensureNotNull(value: B?): B(source)
suspend fun <B> OptionEffectScope.ensureNotNull(value: B?): B(source)
suspend fun <B> OptionEagerEffectScope.ensureNotNull(value: B?): B(source)