Jorge Bo
09/09/2022, 8:45 PMqohatpp
09/11/2022, 5:33 PMEither<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
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()
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.simon.vergauwen
09/12/2022, 7:06 AMi 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.simon.vergauwen
09/12/2022, 7:11 AMEither and then you don't have "this issue".
For example, these two pieces of code are identical.
fun eitherB(): Either<String, Int> =
Left("Can't retrieve two")
And
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.