Skip to content

Commit

Permalink
Merge pull request #538 from paulp/one-and-implicits
Browse files Browse the repository at this point in the history
Adjust implicit priorities in OneAnd.
  • Loading branch information
non committed Sep 23, 2015
2 parents ca3fb05 + b11f414 commit 9393a2d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
20 changes: 11 additions & 9 deletions core/src/main/scala/cats/data/OneAnd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ final case class OneAnd[A, F[_]](head: A, tail: F[A]) {
s"OneAnd(${A.show(head)}, ${FA.show(tail)})"
}

trait OneAndInstances {
trait OneAndInstances extends OneAndLowPriority1 {

implicit def oneAndEq[A, F[_]](implicit A: Eq[A], FA: Eq[F[A]]): Eq[OneAnd[A, F]] =
new Eq[OneAnd[A, F]]{
Expand All @@ -97,12 +97,6 @@ trait OneAndInstances {
implicit def oneAndShow[A, F[_]](implicit A: Show[A], FA: Show[F[A]]): Show[OneAnd[A, F]] =
Show.show[OneAnd[A, F]](_.show)

implicit def oneAndFunctor[F[_]](implicit F: Functor[F]): Functor[OneAnd[?, F]] =
new Functor[OneAnd[?, F]] {
def map[A, B](fa: OneAnd[A, F])(f: A => B): OneAnd[B, F] =
OneAnd(f(fa.head), F.map(fa.tail)(f))
}

implicit def oneAndSemigroupK[F[_]: MonadCombine]: SemigroupK[OneAnd[?, F]] =
new SemigroupK[OneAnd[?, F]] {
def combine[A](a: OneAnd[A, F], b: OneAnd[A, F]): OneAnd[A, F] =
Expand Down Expand Up @@ -137,7 +131,7 @@ trait OneAndInstances {
}
}

trait OneAndLowPriority {
trait OneAndLowPriority0 {
implicit val nelComonad: Comonad[OneAnd[?, List]] =
new Comonad[OneAnd[?, List]] {

Expand All @@ -158,4 +152,12 @@ trait OneAndLowPriority {
}
}

object OneAnd extends OneAndInstances with OneAndLowPriority
trait OneAndLowPriority1 extends OneAndLowPriority0 {
implicit def oneAndFunctor[F[_]](implicit F: Functor[F]): Functor[OneAnd[?, F]] =
new Functor[OneAnd[?, F]] {
def map[A, B](fa: OneAnd[A, F])(f: A => B): OneAnd[B, F] =
OneAnd(f(fa.head), F.map(fa.tail)(f))
}
}

object OneAnd extends OneAndInstances
7 changes: 7 additions & 0 deletions tests/src/test/scala/cats/tests/OneAndTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class OneAndTests extends CatsSuite {
checkAll("Foldable[OneAnd[A, ListWrapper]]", SerializableTests.serializable(Foldable[OneAnd[?, ListWrapper]]))
}

{
// Test functor and subclasses don't have implicit conflicts
implicitly[Functor[NonEmptyList]]
implicitly[Monad[NonEmptyList]]
implicitly[Comonad[NonEmptyList]]
}

checkAll("NonEmptyList[Int]", MonadTests[NonEmptyList].monad[Int, Int, Int])
checkAll("Monad[NonEmptyList[A]]", SerializableTests.serializable(Monad[NonEmptyList]))

Expand Down

0 comments on commit 9393a2d

Please sign in to comment.