cats-effect
cats-effect copied to clipboard
Behaviour of MonadCancelThrow[EitherT[IO, Throwable, *]]
This issue sounds somewhat familiar, and apologies if I forgot the exact discussion, however I stumbled on it in the wild in the context of https://github.com/typelevel/fs2/pull/2895 and found it pretty confusing.
In particular:
- the behaviour of
MonadThrowandMonadCancelThrowis inconsistent - the behaviour of
MonadCancelThrowis weird: excluding cancelation, if something short circuits aflatMapI expect to be able tohandleErrorit. Ofc this expectation is broken when you have two error channels, but MonadThrow works around it
val a: EitherT[IO, Throwable, Unit] =
EitherT.leftT[IO, Unit](new Exception("Yooo"))
val console = Console[EitherT[IO, Throwable, *]]
def foo(fa: EitherT[IO, Throwable, Unit]): EitherT[IO, Throwable, Unit] =
fa.flatMap(_ => console.println("not printed"))
.handleError(_ => ())
def bar[F[_]: MonadThrow: Console](fa: F[Unit]): F[Unit] =
fa.flatMap(_ => Console[F].println("not printed"))
.handleError(_ => ())
def baz[F[_]: MonadCancelThrow: Console](fa: F[Unit]): F[Unit] =
fa.flatMap(_ => Console[F].println("not printed"))
.handleError(_ => ())
def x = foo(a).value.unsafeRunSync()
def y = bar(a).value.unsafeRunSync()
def z = baz(a).value.unsafeRunSync()
scala> x
val res0: Either[Throwable,Unit] = Right(())
scala> y
val res1: Either[Throwable,Unit] = Right(())
scala> z
val res2: Either[Throwable,Unit] = Left(java.lang.Exception: Yooo)