Hello everyone! I use v0.11.0-SNAPSHOT and I encou...
# arrow
s
Hello everyone! I use v0.11.0-SNAPSHOT and I encountered an issue (at least in my understanding):
Copy code
class OneError
class TwoError

private fun oneEither(): Either<OneError, String> = OneError().left()
private fun twoEither(value: String): Either<TwoError, Int> = value.toInt().right()

fun either() {
   oneEither()
     .flatMap { twoEither(it) }
     .fold(
       { throw Exception(it::class.simpleName) },
       { ... }
     )
}

private fun oneIO(): IO<OneError, String> = IO.raiseError(OneError())
private fun twoIO(value: String): IO<TwoError, Int> = IO.just(value.toInt())

fun io() {
   oneIO()
     .flatMap { twoIO(it) }
     .unsafeRunSyncEither()
     .fold(
       { throw Exception(it::class.simpleName) },
       { ... }
     )
}
The function
either
throws
java.lang.Exception: OneError
as I expect. But the function
io
throws
OneError cannot be cast to class TwoError
. When I look at the signature of
flatMap
(
fun <E, A, B, E2 : E> IOOf<E, A>.flatMap(f: (A) -> IOOf<E2, B>): IO<E2, B>
), I think that it shouldn't even compile. Can you tell me if my understanding is correct? I expect that
IO
with two types should behave like
Either
.
p
does it throw or does it fail compilation?
@simon.vergauwen @aballano flatMap should not map the error side AFAIK
s
The function
io
compiles and throws
OneError cannot be cast to class TwoErro
. I think that the left side shouldn't be even touched like in
Either
case.
r
Please create an issue in arrow fx and tag @simon.vergauwen and myself, thanks.
s
👌 1