ensureNotNull

suspend fun <E, B : Any> EitherEffect<E, *>.ensureNotNull(value: B?, orLeft: () -> E): B

Ensures that value is not null. When the value is not null, then it will be returned as non null and the check value is now smart-checked to non-null. Otherwise, if the value is null then the either binding will short-circuit with orLeft inside of Either.Left.

import arrow.core.computations.either
import arrow.core.computations.ensureNotNull

//sampleStart
suspend fun main() {
either<String, Int> {
val x: Int? = 1
ensureNotNull(x) { "passes" }
println(x)
ensureNotNull(null) { "failed" }
}
//sampleEnd
.let(::println)
}
// println: "1"
// res: Either.Left("failed")

suspend fun <B : Any> NullableEffect<*>.ensureNotNull(value: B?): B

Ensures that value is not null. When the value is not null, then it will be returned as non null and the check value is now smart-checked to non-null. Otherwise, if the value is null then the option binding will short-circuit with None.

import arrow.core.computations.nullable
import arrow.core.computations.ensureNotNull

//sampleStart
suspend fun main() {
nullable<Int> {
val x: Int? = 1
ensureNotNull(x)
println(x)
ensureNotNull(null)
}
//sampleEnd
.let(::println)
}
// println: "1"
// res: null

suspend fun <B : Any> OptionEffect<*>.ensureNotNull(value: B?): B

Ensures that value is not null. When the value is not null, then it will be returned as non null and the check value is now smart-checked to non-null. Otherwise, if the value is null then the option binding will short-circuit with None.

import arrow.core.computations.option
import arrow.core.computations.ensureNotNull

//sampleStart
suspend fun main() {
option<Int> {
val x: Int? = 1
ensureNotNull(x)
println(x)
ensureNotNull(null)
}
//sampleEnd
.let(::println)
}
// println: "1"
// res: None