Skip to content

Commit

Permalink
make sure that EitherT MonadError syntax works the old way (#2029)
Browse files Browse the repository at this point in the history
* make sure that EitherT  MonadError syntax works the old way

* added the missing file

* added mima exception
  • Loading branch information
kailuowang authored and LukaJCB committed Nov 14, 2017
1 parent 323f2d5 commit c5f76a2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
}
)

Expand Down
40 changes: 20 additions & 20 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand Down
14 changes: 14 additions & 0 deletions tests/src/test/scala/cats/tests/RegressionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}


}

}

0 comments on commit c5f76a2

Please sign in to comment.