Hello everyone! I use v0.11.0-SNAPSHOT and I encountered an issue (at least in my understanding):
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
.