Hi, I see `Either.fx<L,R>{}` works with only...
# arrow
g
Hi, I see
Either.fx<L,R>{}
works with only one type of
Either<L,R>
. Is there any way to work with different types of
Either
to avoid nested fold?
Copy code
val a: Either<Int, String> = "a".right()
    val b: Either<String, Int> = 1.right()
    
    Either.fx<String, Int> {
        val bind = a.bind() // ^^^ Compiler Error
        b.bind()
    }
s
Hi, that doesn’t work since
L
is where
flatMap
works over.
Or rather it short-circuits on
L
being present or in other words it can only return
L
as a final result.
fun <A, B, C> EitherOf<A, B>.flatMap(f:(B) -> Either<A, C>): Either<A, C>
So this code is
a.flatMap { b }
which doesn’t compile
g
Yes @simon.vergauwen I understand why the error is, but I am looking for any concise way to work around the problem, so I don’t have to write nested folds
Copy code
val fold = a.fold(
            { it.toString() },
            {
                b.fold(
                        { it },
                        { it.toString() }
                )
            }
    )
s
Other functions such as
flatMapLeft
can still be implemented / contributed to Arrow Core 😉
Then you can do
Copy code
Either.fx<String, Int> {
        val bind = a.mapLeft { it.toString() }.bind()
        b.bind()
    }
g
Thanks @simon.vergauwen