Skip to content

Commit

Permalink
Fix ambiguity when the base type has both Semigroup and CommutativeSe…
Browse files Browse the repository at this point in the history
…migroup
  • Loading branch information
barambani committed Jun 10, 2020
1 parent 1d93305 commit 27741bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ class Tests extends TestsConfig with AnyFunSuiteLike with FunSuiteDiscipline wit

import KernelCheck._

test("The instances in scope are not ambiguous") {
implicitly[Monoid[Option[String]]]
implicitly[Semigroup[Option[String]]]
implicitly[Monoid[Option[Int]]]
implicitly[Semigroup[Option[Int]]]
implicitly[CommutativeSemigroup[Option[Int]]]
implicitly[CommutativeMonoid[Option[Int]]]
}

{
// needed for Cogen[Map[...]]
implicit val ohe: Ordering[HasEq[Int]] = Ordering.by[HasEq[Int], Int](_.a)
Expand Down
11 changes: 6 additions & 5 deletions kernel/src/main/scala/cats/kernel/Semigroup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ object Semigroup

implicit def catsKernelMonoidForString: Monoid[String] = cats.kernel.instances.string.catsKernelStdMonoidForString

implicit def catsKernelMonoidForOption[A: Semigroup]: Monoid[Option[A]] =
cats.kernel.instances.option.catsKernelStdMonoidForOption[A]
implicit def catsKernelMonoidForList[A]: Monoid[List[A]] = cats.kernel.instances.list.catsKernelStdMonoidForList[A]
implicit def catsKernelMonoidForVector[A]: Monoid[Vector[A]] =
cats.kernel.instances.vector.catsKernelStdMonoidForVector[A]
Expand Down Expand Up @@ -230,6 +228,8 @@ private[kernel] trait CommutativeMonoidInstances extends MonoidInstances {
cats.kernel.instances.function.catsKernelCommutativeMonoidForFunction0[A]
implicit def catsKernelCommutativeMonoidForFunction1[A, B: CommutativeMonoid]: CommutativeMonoid[A => B] =
cats.kernel.instances.function.catsKernelCommutativeMonoidForFunction1[A, B]
implicit def catsKernelCommutativeMonoidForOption[A: CommutativeSemigroup]: CommutativeMonoid[Option[A]] =
cats.kernel.instances.option.catsKernelStdCommutativeMonoidForOption[A]
}

private[kernel] trait MonoidInstances extends BandInstances {
Expand All @@ -243,7 +243,10 @@ private[kernel] trait MonoidInstances extends BandInstances {
cats.kernel.instances.sortedMap.catsKernelStdMonoidForSortedMap[K, V]
implicit def catsKernelMonoidForEither[A, B: Monoid]: Monoid[Either[A, B]] =
cats.kernel.instances.either.catsDataMonoidForEither[A, B]
implicit def catsKernelMonoidForTry[A: Monoid]: Monoid[Try[A]] = new TryMonoid[A](Monoid[A])
implicit def catsKernelMonoidForTry[A: Monoid]: Monoid[Try[A]] =
new TryMonoid[A](Monoid[A])
implicit def catsKernelMonoidForOption[A: Semigroup]: Monoid[Option[A]] =
cats.kernel.instances.option.catsKernelStdMonoidForOption[A]
}

private[kernel] trait BandInstances extends CommutativeSemigroupInstances {
Expand All @@ -269,8 +272,6 @@ private[kernel] trait SemigroupInstances {
cats.kernel.instances.either.catsDataSemigroupForEither[A, B]
implicit def catsKernelSemigroupForTry[A: Semigroup]: Semigroup[Try[A]] =
new TrySemigroup[A](Semigroup[A])
implicit def catsKernelCommutativeMonoidForOption[A: CommutativeSemigroup]: CommutativeMonoid[Option[A]] =
cats.kernel.instances.option.catsKernelStdCommutativeMonoidForOption[A]
}

private class TryMonoid[A](A: Monoid[A]) extends TrySemigroup[A](A) with Monoid[Try[A]] {
Expand Down

0 comments on commit 27741bd

Please sign in to comment.