From 32a99fda8c4e2991a86540f8cd1453cf0892eb95 Mon Sep 17 00:00:00 2001 From: Kevin Lee Date: Sun, 8 Aug 2021 00:54:20 +1000 Subject: [PATCH] Add CanEqual instance for Seq to match Nil case --- library/src/scala/CanEqual.scala | 6 ++++-- tests/neg/derive-eq.scala | 2 +- tests/neg/equality1.scala | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/library/src/scala/CanEqual.scala b/library/src/scala/CanEqual.scala index e65c8e3909b5..dfb4ec7d2bfc 100644 --- a/library/src/scala/CanEqual.scala +++ b/library/src/scala/CanEqual.scala @@ -26,10 +26,12 @@ object CanEqual { given canEqualNumber: CanEqual[Number, Number] = derived given canEqualString: CanEqual[String, String] = derived - // The next five definitions can go into the companion objects of their corresponding + // The next 6 definitions can go into the companion objects of their corresponding // classes. For now they are here in order not to have to touch the // source code of these classes - given canEqualSeq[T, U](using eq: CanEqual[T, U]): CanEqual[Seq[T], Seq[U]] = derived + given canEqualSeqs[T, U](using eq: CanEqual[T, U]): CanEqual[Seq[T], Seq[U]] = derived + given canEqualSeq[T](using eq: CanEqual[T, T]): CanEqual[Seq[T], Seq[T]] = derived // for `case Nil` in pattern matching + given canEqualSet[T, U](using eq: CanEqual[T, U]): CanEqual[Set[T], Set[U]] = derived given canEqualOptions[T, U](using eq: CanEqual[T, U]): CanEqual[Option[T], Option[U]] = derived diff --git a/tests/neg/derive-eq.scala b/tests/neg/derive-eq.scala index fa73cf63a5bf..2a9e35a24a19 100644 --- a/tests/neg/derive-eq.scala +++ b/tests/neg/derive-eq.scala @@ -20,10 +20,10 @@ object Test extends App { val y: Triple[List[Two], One, Two] = ??? val z: Triple[One, List[Two], One] = ??? x == y // OK + y == x // OK x == x // OK y == y // OK - y == x // error x == z // error z == y // error } diff --git a/tests/neg/equality1.scala b/tests/neg/equality1.scala index 1e7b76908d79..74bd45b18c12 100644 --- a/tests/neg/equality1.scala +++ b/tests/neg/equality1.scala @@ -117,4 +117,19 @@ object equality1 { (1, "a") == (1, "a", true) // error: cannot compare (1, "a", true, 't', 10L) == (1, "a", 1.5D, 't', 10L) // error: cannot compare + + val ns1 = List(1, 2, 3, 4, 5) + val ns2 = List(1, 2, 3, 4, 5) + ns1 == ns2 + + val ss = List("1", "2", "3", "4", "5") + ns1 == ss // error: cannot compare + + ns1 match { + case n :: ns => + println(s"head: $n, tail: ${ns.mkString("[", ",", "]")}") + case Nil => + println("empty") + } + }