Skip to content

Commit

Permalink
Changed precedence of tupled instances for Hash and PartialOrder (#3105)
Browse files Browse the repository at this point in the history
  • Loading branch information
barambani authored and LukaJCB committed Oct 16, 2019
1 parent eb52c0b commit 6e7ff8f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Thank you for contributing to Cats!

This is a kind reminder to run `sbt prePR` and commit the changed files, if any, before submitting.
This is a kind reminder to run `sbt +prePR` and commit the changed files, if any, before submitting.


12 changes: 6 additions & 6 deletions project/KernelBoiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,6 @@ object KernelBoiler {
.mkString(s"$tupleNHeader(", ", ", ")")}.hashCode()
def eqv(x: ${`(A..N)`}, y: ${`(A..N)`}): Boolean = ${binMethod("eqv").mkString(" && ")}
}

implicit def catsKernelStdPartialOrderForTuple${arity}[${`A..N`}](implicit ${constraints("PartialOrder")}): PartialOrder[${`(A..N)`}] =
new PartialOrder[${`(A..N)`}] {
def partialCompare(x: ${`(A..N)`}, y: ${`(A..N)`}): Double =
${binMethod("partialCompare").mkString("Array(", ", ", ")")}.find(_ != 0.0).getOrElse(0.0)
}
"""
}
),
Expand All @@ -217,6 +211,12 @@ object KernelBoiler {
import tv._
def content =
block"""
implicit def catsKernelStdPartialOrderForTuple${arity}[${`A..N`}](implicit ${constraints("PartialOrder")}): PartialOrder[${`(A..N)`}] =
new PartialOrder[${`(A..N)`}] {
def partialCompare(x: ${`(A..N)`}, y: ${`(A..N)`}): Double =
${binMethod("partialCompare").mkString("Array(", ", ", ")")}.find(_ != 0.0).getOrElse(0.0)
}

implicit def catsKernelStdBandForTuple${arity}[${`A..N`}](implicit ${constraints("Band")}): Band[${`(A..N)`}] =
new Band[${`(A..N)`}] {
def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
Expand Down
22 changes: 22 additions & 0 deletions tests/src/test/scala/cats/tests/EqSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import cats.laws.discipline.{ContravariantMonoidalTests, MiniInt}
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.eq._

import scala.collection.immutable.Queue

class EqSuite extends CatsSuite {
Invariant[Eq]
Contravariant[Eq]
Expand All @@ -15,4 +17,24 @@ class EqSuite extends CatsSuite {
checkAll("Eq", ContravariantMonoidalTests[Eq].contravariantMonoidal[MiniInt, Boolean, Boolean])
checkAll("ContravariantMonoidal[Eq]", SerializableTests.serializable(ContravariantMonoidal[Eq]))

test("The Eq instance for tuples of A is not ambiguous when A has a Hash and a PartialOrder") {

import cats.kernel.{Hash, PartialOrder}

trait A
implicit def po: PartialOrder[A] = ???
implicit def ho: Hash[A] = ???

lazy val a2 = implicitly[Eq[(A, A)]]
lazy val b2 = implicitly[Eq[(List[A], List[A])]]
lazy val c2 = implicitly[Eq[(Set[A], Set[A])]]
lazy val d2 = implicitly[Eq[(Vector[A], Vector[A])]]
lazy val e2 = implicitly[Eq[(Queue[A], Queue[A])]]

lazy val a3 = implicitly[Eq[(A, A, A)]]
lazy val b3 = implicitly[Eq[(List[A], List[A], List[A])]]
lazy val c3 = implicitly[Eq[(Set[A], Set[A], Set[A])]]
lazy val d3 = implicitly[Eq[(Vector[A], Vector[A], Vector[A])]]
lazy val e3 = implicitly[Eq[(Queue[A], Queue[A], Queue[A])]]
}
}

0 comments on commit 6e7ff8f

Please sign in to comment.