typelevel / typelevel/cats

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

Open
#4,308 7 comments 0 reactions 1 assignee View on GitHub

@armanbilge is already working on this.

Since Jun 12, 2022.

Dominant language
Scala
Stars
5.5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
5

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.