From 70d2c44baf7798a9d5627a312d344e7095784676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Mon, 24 Apr 2017 10:10:25 +0100 Subject: [PATCH] improve EitherT inference for use in for-comprehension --- core/src/main/scala/cats/data/EitherT.scala | 6 +++--- tests/src/test/scala/cats/tests/EitherTTests.scala | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/src/main/scala/cats/data/EitherT.scala b/core/src/main/scala/cats/data/EitherT.scala index 1b51488624..8f7b6149c7 100644 --- a/core/src/main/scala/cats/data/EitherT.scala +++ b/core/src/main/scala/cats/data/EitherT.scala @@ -71,19 +71,19 @@ final case class EitherT[F[_], A, B](value: F[Either[A, B]]) { def applyAlt[D](ff: EitherT[F, A, B => D])(implicit F: Apply[F]): EitherT[F, A, D] = EitherT[F, A, D](F.map2(this.value, ff.value)((xb, xbd) => Apply[Either[A, ?]].ap(xbd)(xb))) - def flatMap[AA >: A, D](f: B => EitherT[F, AA, D])(implicit F: Monad[F]): EitherT[F, AA, D] = + def flatMap[D](f: B => EitherT[F, A, D])(implicit F: Monad[F]): EitherT[F, A, D] = EitherT(F.flatMap(value) { case l @ Left(_) => F.pure(l.rightCast) case Right(b) => f(b).value }) - def flatMapF[AA >: A, D](f: B => F[Either[AA, D]])(implicit F: Monad[F]): EitherT[F, AA, D] = + def flatMapF[D](f: B => F[Either[A, D]])(implicit F: Monad[F]): EitherT[F, A, D] = flatMap(f andThen EitherT.apply) def transform[C, D](f: Either[A, B] => Either[C, D])(implicit F: Functor[F]): EitherT[F, C, D] = EitherT(F.map(value)(f)) - def subflatMap[AA >: A, D](f: B => Either[AA, D])(implicit F: Functor[F]): EitherT[F, AA, D] = + def subflatMap[D](f: B => Either[A, D])(implicit F: Functor[F]): EitherT[F, A, D] = transform(_.flatMap(f)) def map[D](f: B => D)(implicit F: Functor[F]): EitherT[F, A, D] = bimap(identity, f) diff --git a/tests/src/test/scala/cats/tests/EitherTTests.scala b/tests/src/test/scala/cats/tests/EitherTTests.scala index dbe79220ce..a611d12d26 100644 --- a/tests/src/test/scala/cats/tests/EitherTTests.scala +++ b/tests/src/test/scala/cats/tests/EitherTTests.scala @@ -378,4 +378,11 @@ class EitherTTests extends CatsSuite { } } } + test("inference works in for-comprehension") { + val either: Id[Either[String, Int]] = 0.asRight.pure[Id] + for { + a <- EitherT(either) + b <- EitherT.right(1.pure[Id]) + } yield (a + b) + } }