common inline fun <A, B> Either<A, B?>.leftIfNull(default: () -> A): Either<A, B>

Returns Right with the existing value of Right if this is an Right with a non-null value. The returned Either.Right type is not nullable.

Returns Left(default()) if this is an Right and the existing value is null

Returns Left with the existing value of Left if this is an Left.


import arrow.core.Either.*
import arrow.core.leftIfNull

fun main() {
  Right(12).leftIfNull({ -1 })   // Result: Right(12)
  Right(null).leftIfNull({ -1 }) // Result: Left(-1)

  Left(12).leftIfNull({ -1 })    // Result: Left(12)

Do you like Arrow?

Arrow Org