Hi everyone, I had a use case recently where I had...
# arrow
g
Hi everyone, I had a use case recently where I had to return left in an either if the right value was not null. So I ended up writing an extension function for that:
inline fun <E, B> Either<E, B?>.leftIfNotNull(f: () -> E): Either<E, Nothing?> = flatMap { it.rightIfNull { f() } }
Is there something that might have helped in Arrow and I've missed it?
t
If you use either effect, I usually use this pattern
Copy code
fun loadValue(): Either<Error, Value?> = TODO()
either {  
  val valueOrNull = loadValue().bind()
  val value = ensureNotNull(valueOrNull) { ValueNotFound }
}
g
So in my case it would be something like this
Copy code
ensure(valueOrNull == null) { "Value already exists" }
Thank you
👍 1