Skip to content

Commit

Permalink
Fix some implicit resolution issues in OptionT.
Browse files Browse the repository at this point in the history
Add a "test" to check the implicit resolution of OptionT instances.
  • Loading branch information
peterneyens committed Jun 14, 2016
1 parent c3cae9e commit 1dffb84
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
22 changes: 13 additions & 9 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,9 @@ private[data] sealed trait OptionTInstances extends OptionTInstances0 {
}

private[data] sealed trait OptionTInstances0 extends OptionTInstances1 {
implicit def catsDataMonadForOptionT[F[_]](implicit F0: Monad[F]): Monad[OptionT[F, ?]] =
new OptionTMonad[F] { 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 catsDataTraverseForOptionT[F[_]](implicit F0: Traverse[F]): Traverse[OptionT[F, ?]] =
new OptionTTraverse[F] { implicit val F = F0 }

implicit def catsDataSemigroupK[F[_]](implicit F0: Monad[F]): SemigroupK[OptionT[F, ?]] =
new OptionTSemigroupK[F] { implicit val F = F0 }

Expand All @@ -182,9 +176,9 @@ private[data] sealed trait OptionTInstances0 extends OptionTInstances1 {
new OptionTPartialOrder[F, A] { implicit val F = F0 }
}

private[data] sealed trait OptionTInstances1 {
implicit def catsDataFunctorForOptionT[F[_]](implicit F0: Functor[F]): Functor[OptionT[F, ?]] =
new OptionTFunctor[F] { implicit val F = F0 }
private[data] sealed trait OptionTInstances1 extends OptionTInstances2 {
implicit def catsDataMonadForOptionT[F[_]](implicit F0: Monad[F]): Monad[OptionT[F, ?]] =
new OptionTMonad[F] { implicit val F = F0 }

// do NOT change this to val! I know it looks like it should work, and really I agree, but it doesn't (for... reasons)
implicit def catsDataTransLiftForOptionT: TransLift.Aux[OptionT, Functor] =
Expand All @@ -201,6 +195,16 @@ private[data] sealed trait OptionTInstances1 {
new OptionTEq[F, A] { implicit val F = F0 }
}

private[data] sealed trait OptionTInstances2 extends OptionTInstances3 {
implicit def catsDataTraverseForOptionT[F[_]](implicit F0: Traverse[F]): Traverse[OptionT[F, ?]] =
new OptionTTraverse[F] { implicit val F = F0 }
}

private[data] sealed trait OptionTInstances3 {
implicit def catsDataFunctorForOptionT[F[_]](implicit F0: Functor[F]): Functor[OptionT[F, ?]] =
new OptionTFunctor[F] { implicit val F = F0 }
}

private[data] trait OptionTFunctor[F[_]] extends Functor[OptionT[F, ?]] {
implicit def F: Functor[F]

Expand Down
32 changes: 32 additions & 0 deletions tests/src/test/scala/cats/tests/OptionTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,36 @@ class OptionTTests extends CatsSuite {
}
}


/**
* Testing that implicit resolution works. If it compiles, the "test" passes.
*/
object ImplicitResolution{
Eq[OptionT[List, Int]]
PartialOrder[OptionT[List, Int]]
Order[OptionT[List, Int]]

Semigroup[OptionT[List, Int]]
Monoid[OptionT[List, Int]]

SemigroupK[OptionT[List, ?]]
MonoidK[OptionT[List, ?]]

Functor[OptionT[List, ?]]
Monad[OptionT[List, ?]]
MonadRec[OptionT[List, ?]]

import scala.util.Try
Functor[OptionT[Try, ?]]
Monad[OptionT[Try, ?]]
MonadError[OptionT[Try, ?], Throwable]

Foldable[OptionT[List, ?]]
Traverse[OptionT[List, ?]]

implicit val T = ListWrapper.traverse
implicit val M = ListWrapper.monad
Functor[OptionT[ListWrapper, ?]]
}

}

0 comments on commit 1dffb84

Please sign in to comment.