cats-effect icon indicating copy to clipboard operation
cats-effect copied to clipboard

Behaviour of MonadCancelThrow[EitherT[IO, Throwable, *]]

Open SystemFw opened this issue 3 years ago • 5 comments

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 MonadThrow and MonadCancelThrow is inconsistent
  • the behaviour of MonadCancelThrow is weird: excluding cancelation, if something short circuits a flatMap I expect to be able to handleError it. 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)

SystemFw avatar May 10 '22 00:05 SystemFw