Skotar
04/24/2020, 8:27 PMclass 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
.pakoito
04/25/2020, 2:58 AMpakoito
04/25/2020, 2:58 AMSkotar
04/25/2020, 7:19 AMio
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.raulraja
04/25/2020, 2:47 PMSkotar
04/25/2020, 9:44 PM