Hey, i’m having problems to understand where bind ...
# arrow
j
Hey, i’m having problems to understand where bind inside either block should be called, i realize that sometimes i forgot to call it and the code basically does not execute.
q
Well, In not very theoretical words, for an
Either<A, B>
,
bind
is the mechanism for getting the pure value
B
(the right one) or short circuit with the value
A
(the left one) So using
bind()
, the happy path
Copy code
fun eitherA = Right(1)
fun eitherB = Right(2)
fun eitherC = Right(3)

val sum: Either<String, Int> = either {
  val a = eitherA.bind()
  val b = eitherB.bind()
  val c = eitherC.bind()
  a + b + c
} 

// sum = Right(6)
Unhappy path
bind()
Copy code
fun eitherA = Right(1)
fun eitherB = Left("Can't retrieve two")
fun eitherC = Right(3)

val sum: Either<String, Int> = either {
  val a = eitherA.bind()
  val b = eitherB.bind() //Short circuit with String
  val c = eitherC.bind()
  a + b + c
}

// sum = Left("Can't retrieve two")
As you can see I call
bind()
inside my
effect scope (either block)
whenever I need a pure
Int
to add with others, even though I know the bind might end up being a
String
in the error case.
s
i realize that sometimes i forgot to call it and the code basically does not execute.
This is indeed a problem, but it's a similar problem with forgetting to call
get()
on a
Future
when it needs to be awaited, or similar. This can sadly only be fixed by something like linting atm. In the future when the Kotlin Compiler allows us to send hints to IntelliJ, and warnings to the CLI we will most likely add support for warnings and errors to Arrow. Such as this use-case. This problem typically only arises if you ignore the output of your function since it would not be possible to call
+
on
Either
in @qohatpp his example.
Alternatively, you could also encode your code without returning
Either
and then you don't have "this issue". For example, these two pieces of code are identical.
Copy code
fun eitherB(): Either<String, Int> =
  Left("Can't retrieve two")
And
Copy code
suspend fun EffectScope<String>.eitherB2(): Int =
  Left("Can't retrieve two").bind()
In the second one instead of returning
Either<String, Int>
we have a receiver of
EffectScope<String>
which can call
bind
on any
Either
that has
String
for the
Either.Left
type argument. Like in @qohatpp his example above. Since we can call
bind
directly inside the function now, we can simply return
Int
.