Hello :wave: Is this the idiomatic way of `bind`in...
# arrow
j
Hello 👋 Is this the idiomatic way of `bind`ing
Either
and
EitherNel
in the same
either { … }
block?
Copy code
fun foo(): Either<E, A>
fun A.bar(): EitherNel<E, B>

fun baz(): EitherNel<E, B> = either {
    val a = foo().toEitherNel().bind()
    a.bar().bind()
}
Thanks in advance 😄
👋 4
s
Hey James, Sadly Kotlin cannot have specific support for this, but once we have context receivers we can define:
Copy code
context(Raise<NonEmptyList<E>>)
fun Either<E, A>.bind(): A = when(this) {
  is Right -> value
  is Left -> raise(nonEmptyListOf(value))
}
y
Once withError makes it into a release (it's already merged, just not released yet) you can also do:
Copy code
fun baz(): EitherNel<E, B> = either {
  withError({ e: E -> e.nel() }) {
    val a = foo().bind()
    a.bar().bind()
  }
}
j
I've just started using
withError
in 1.2.0 Cheers!
🙌 1
arrow intensifies 2
y
Also you can define a method that does the
withError({ e: E -> e.nel() }) {}
wrapping for you. Call it
withNel
or something