Is Arrow having something like zip function for nu...
# arrow
m
Is Arrow having something like zip function for nullable Kotlin variables? Solving this problem: https://stackoverflow.com/questions/35513636/multiple-variable-let-in-kotlin
t
it does. It's
Nullable.zip
🙂
m
Ah, I was looking at autocomplete methods on the nullable variable. Maybe that is not allowed to define? Addet it to SO.
y
It's probably completely valid to define it, but I'm guessing it'll pollute the autocomplete for all Kotlin variables, since anything defined on
Any?
also accepts
Any
t
Not just pollute, there's gonna be no way to distinguish which function to call
Copy code
val e1: Either<A,B>
val e2: Either<A,B>
e1.zip(e2, ::Pair)
y
Isn't it the case that extension funs are resolved based on the most specific receiver type?, so actually that case should work, but it'll be confusing if you have a nullable either
t
try running this:
Copy code
import arrow.core.Either
import arrow.core.zip

inline fun <A,B, R> A?.zip(b: B?, f: (A, B) -> R): R? =
    this?.let{ aa -> b?.let{bb -> f(aa,bb)}}

val e1 = Either.Right(1)
val e2 = Either.Left(Unit)

null.zip(2, ::Pair).let(::println)
e1.zip(e2, ::Pair).let(::println)
it's gonna print
Copy code
null // as expected
(Either.Right(1), Either.Left(kotlin.Unit)) // yikes :D
250 Views