From c5f76a243071545675c9b7a1bd06251c41bca658 Mon Sep 17 00:00:00 2001 From: "Kai(luo) Wang" Date: Tue, 14 Nov 2017 13:52:00 -0500 Subject: [PATCH] make sure that EitherT MonadError syntax works the old way (#2029) * make sure that EitherT MonadError syntax works the old way * added the missing file * added mima exception --- build.sbt | 7 +++- core/src/main/scala/cats/data/EitherT.scala | 40 +++++++++---------- ...Tests.scala => ExtraRegressionSuite.scala} | 0 .../scala/cats/tests/RegressionSuite.scala | 14 +++++++ 4 files changed, 39 insertions(+), 22 deletions(-) rename tests/src/test/scala-2.11+/cats/tests/{RegressionTests.scala => ExtraRegressionSuite.scala} (100%) diff --git a/build.sbt b/build.sbt index 9ed50b92be..f68e160395 100644 --- a/build.sbt +++ b/build.sbt @@ -190,10 +190,13 @@ lazy val binaryCompatibleVersion = "1.0.0-RC1" def mimaSettings(moduleName: String) = Seq( mimaPreviousArtifacts := Set("org.typelevel" %% moduleName % binaryCompatibleVersion), // TODO: remove this post-release of 1.0.0 - mimaBinaryIssueFilters += { + mimaBinaryIssueFilters ++= { import com.typesafe.tools.mima.core._ import com.typesafe.tools.mima.core.ProblemFilters._ - exclude[ReversedMissingMethodProblem]("cats.syntax.FoldableSyntax.catsSyntaxFoldOps") + Seq( + exclude[ReversedMissingMethodProblem]("cats.syntax.FoldableSyntax.catsSyntaxFoldOps"), + exclude[DirectMissingMethodProblem]("cats.data.EitherTInstances2.catsDataMonadErrorForEitherT") + ) } ) diff --git a/core/src/main/scala/cats/data/EitherT.scala b/core/src/main/scala/cats/data/EitherT.scala index 85ac89d315..090888ca84 100644 --- a/core/src/main/scala/cats/data/EitherT.scala +++ b/core/src/main/scala/cats/data/EitherT.scala @@ -483,26 +483,6 @@ private[data] abstract class EitherTInstances1 extends EitherTInstances2 { val F0: Traverse[F] = F } - /** Monad error instance for recovering errors in F instead of - * the underlying Either. - * - * {{{ - * scala> import cats.data.EitherT - * scala> import cats.MonadError - * scala> import cats.instances.option._ - * scala> val noInt: Option[Either[String, Int]] = None - * scala> val et = EitherT[Option, String, Int](noInt) - * scala> val me = MonadError[EitherT[Option, String, ?], Unit] - * scala> me.recover(et) { case () => 1 } - * res0: cats.data.EitherT[Option,String,Int] = EitherT(Some(Right(1))) - * }}} - */ - implicit def catsDataMonadErrorFForEitherT[F[_], E, L](implicit FE0: MonadError[F, E]): MonadError[EitherT[F, L, ?], E] = - new EitherTMonadErrorF[F, E, L] { implicit val F = FE0 } -} - -private[data] abstract class EitherTInstances2 extends EitherTInstances3 { - implicit def catsDataMonadErrorForEitherT[F[_], L](implicit F0: Monad[F]): MonadError[EitherT[F, L, ?], L] = new EitherTMonadError[F, L] { implicit val F = F0 @@ -512,6 +492,26 @@ private[data] abstract class EitherTInstances2 extends EitherTInstances3 { override def ensureOr[A](fa: EitherT[F, L, A])(error: (A) => L)(predicate: (A) => Boolean): EitherT[F, L, A] = fa.ensureOr(error)(predicate)(F) } +} + +private[data] abstract class EitherTInstances2 extends EitherTInstances3 { + /** Monad error instance for recovering errors in F instead of + * the underlying Either. + * + * {{{ + * scala> import cats.data.EitherT + * scala> import cats.MonadError + * scala> import cats.instances.option._ + * scala> val noInt: Option[Either[String, Int]] = None + * scala> val et = EitherT[Option, String, Int](noInt) + * scala> val me = MonadError[EitherT[Option, String, ?], Unit] + * scala> me.recover(et) { case () => 1 } + * res0: cats.data.EitherT[Option,String,Int] = EitherT(Some(Right(1))) + * }}} + */ + implicit def catsDataMonadErrorFForEitherT[F[_], E, L](implicit FE0: MonadError[F, E]): MonadError[EitherT[F, L, ?], E] = + new EitherTMonadErrorF[F, E, L] { implicit val F = FE0 } + implicit def catsDataSemigroupKForEitherT[F[_], L](implicit F0: Monad[F]): SemigroupK[EitherT[F, L, ?]] = new EitherTSemigroupK[F, L] { implicit val F = F0 } diff --git a/tests/src/test/scala-2.11+/cats/tests/RegressionTests.scala b/tests/src/test/scala-2.11+/cats/tests/ExtraRegressionSuite.scala similarity index 100% rename from tests/src/test/scala-2.11+/cats/tests/RegressionTests.scala rename to tests/src/test/scala-2.11+/cats/tests/ExtraRegressionSuite.scala diff --git a/tests/src/test/scala/cats/tests/RegressionSuite.scala b/tests/src/test/scala/cats/tests/RegressionSuite.scala index 570b7691a8..59cb4cb27f 100644 --- a/tests/src/test/scala/cats/tests/RegressionSuite.scala +++ b/tests/src/test/scala/cats/tests/RegressionSuite.scala @@ -123,4 +123,18 @@ class RegressionSuite extends CatsSuite { checkAndResetCount(1) } + test("#2022 EitherT syntax no long works the old way") { + import data._ + + + EitherT.right[String](Option(1)).handleErrorWith((_: String) => EitherT.pure(2)) + + { + implicit val me = MonadError[EitherT[Option, String, ?], Unit] + EitherT.right[String](Option(1)).handleErrorWith((_: Unit) => EitherT.pure(2)) + } + + + } + }