You import two instances of Semigroup[Int]
:
- via
import cats.implicits._
- via
import SemigroupInstances._
The compiler chooses the one from cats.implicits._
over your custom instance.
If you remove that import, it works: https://scastie.scala-lang.org/avsmEturTRGkNEOCDxuXOA
If you want to know more about how the compiler chooses which implicit instance to use, see this post: Where does Scala look for implicits?
In your specific case, the reason appears to be that the type of the instance from cats is actually CommutativeGroup[Int]
:
implicit val catsKernelStdGroupForInt: CommutativeGroup[Int] = new IntGroup
Hence, being a derived class (CommutativeGroup
extends Semigroup
), it is considered more specific and hence preferred.
If you change the type of your instance to CommutativeGroup[Int]
(which is not possible without being mathematically incorrect, but that's not the point here), you get an "Ambiguous given instances" error because the compiler cannot decide which instance to use: https://scastie.scala-lang.org/dzClDOYDSJ20P1SRpH72nA