Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2611 - MonadError syntax for OptionT uses incorrect instance #2615

Merged
merged 5 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion binCompatTest/src/main/scala/catsBC/MimaExceptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ object MimaExceptions {
cats.data.Kleisli.catsDataCommutativeFlatMapForKleisli[Option, Int],
cats.data.IRWST.catsDataStrongForIRWST[List, Int, Int, Int],
cats.data.OptionT.catsDataMonadErrorMonadForOptionT[List],
FunctionK.lift(headOption)
FunctionK.lift(headOption),
cats.data.OptionT.catsDataMonadErrorForOptionT[Either[String, ?], String],
cats.data.OptionT[Either[String, ?], Int](Right(Some(17))).ensure("error")(_ => true)
)

}
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ def mimaSettings(moduleName: String) =
exclude[DirectMissingMethodProblem]("cats.data.KleisliInstances1.catsDataCommutativeArrowForKleisli"),
exclude[DirectMissingMethodProblem]("cats.data.KleisliInstances4.catsDataCommutativeFlatMapForKleisli"),
exclude[DirectMissingMethodProblem]("cats.data.IRWSTInstances1.catsDataStrongForIRWST"),
exclude[DirectMissingMethodProblem]("cats.data.OptionTInstances1.catsDataMonadErrorMonadForOptionT")
exclude[DirectMissingMethodProblem]("cats.data.OptionTInstances1.catsDataMonadErrorMonadForOptionT"),
exclude[DirectMissingMethodProblem]("cats.data.OptionTInstances1.catsDataMonadErrorForOptionT")
) ++ // Only compile-time abstractions (macros) allowed here
Seq(
exclude[IncompatibleMethTypeProblem]("cats.arrow.FunctionKMacros.lift"),
Expand Down Expand Up @@ -562,7 +563,7 @@ lazy val binCompatTest = project
.disablePlugins(CoursierPlugin)
.settings(noPublishSettings)
.settings(
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.7"),
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.8"),
libraryDependencies ++= List(
{
if (priorTo2_13(scalaVersion.value))
Expand Down
16 changes: 12 additions & 4 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,16 @@ sealed abstract private[data] class OptionTInstances extends OptionTInstances0 {
}

sealed abstract private[data] class OptionTInstances0 extends OptionTInstances1 {
implicit def catsDataMonadErrorMonadForOptionT[F[_]](implicit F0: Monad[F]): MonadError[OptionT[F, ?], Unit] =
new OptionTMonadErrorMonad[F] { implicit val F = F0 }

// the Dummy type is to make this one more specific than catsDataMonadErrorMonadForOptionT on 2.13.x
// see https://github.com/typelevel/cats/pull/2335#issuecomment-408249775
implicit def catsDataMonadErrorForOptionT[F[_], E](
implicit F0: MonadError[F, E]
): MonadError[OptionT[F, ?], E] { type Dummy } =
new OptionTMonadError[F, E] {
type Dummy
implicit val F = F0
}

implicit def catsDataContravariantMonoidalForOptionT[F[_]](
implicit F0: ContravariantMonoidal[F]
Expand Down Expand Up @@ -303,8 +311,8 @@ sealed abstract private[data] class OptionTInstances1 extends OptionTInstances2
implicit def catsDataEqForOptionT[F[_], A](implicit F0: Eq[F[Option[A]]]): Eq[OptionT[F, A]] =
new OptionTEq[F, A] { implicit val F = F0 }

implicit def catsDataMonadErrorForOptionT[F[_], E](implicit F0: MonadError[F, E]): MonadError[OptionT[F, ?], E] =
new OptionTMonadError[F, E] { implicit val F = F0 }
implicit def catsDataMonadErrorMonadForOptionT[F[_]](implicit F0: Monad[F]): MonadError[OptionT[F, ?], Unit] =
new OptionTMonadErrorMonad[F] { implicit val F = F0 }
}

sealed abstract private[data] class OptionTInstances2 extends OptionTInstances3 {
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/OptionTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ class OptionTSuite extends CatsSuite {
checkAll("MonadError[OptionT[List, ?]]", SerializableTests.serializable(MonadError[OptionT[ListWrapper, ?], Unit]))
}

test("MonadError[OptionT[F, ?], E] instance has higher priority than MonadError[OptionT[F, ?], Unit]") {

def shouldCompile[F[_]: MonadError[?[_], E], E](x: OptionT[F, Int], e: E): OptionT[F, Int] =
x.ensure(e)(_ => true)
}

test("fold and cata consistent") {
forAll { (o: OptionT[List, Int], s: String, f: Int => String) =>
o.fold(s)(f) should ===(o.cata(s, f))
Expand Down