Skip to content

Commit 665df86

Browse files
committed
Add CanEqual instance for Seq to match Nil case
1 parent 408e6d2 commit 665df86

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

library/src/scala/CanEqual.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ object CanEqual {
2626
given canEqualNumber: CanEqual[Number, Number] = derived
2727
given canEqualString: CanEqual[String, String] = derived
2828

29-
// The next five definitions can go into the companion objects of their corresponding
29+
// The next 6 definitions can go into the companion objects of their corresponding
3030
// classes. For now they are here in order not to have to touch the
3131
// source code of these classes
32-
given canEqualSeq[T, U](using eq: CanEqual[T, U]): CanEqual[Seq[T], Seq[U]] = derived
32+
given canEqualSeqs[T, U](using eq: CanEqual[T, U]): CanEqual[Seq[T], Seq[U]] = derived
33+
given canEqualSeq[T](using eq: CanEqual[T, T]): CanEqual[Seq[T], Seq[T]] = derived // for `case Nil` in pattern matching
34+
3335
given canEqualSet[T, U](using eq: CanEqual[T, U]): CanEqual[Set[T], Set[U]] = derived
3436

3537
given canEqualOptions[T, U](using eq: CanEqual[T, U]): CanEqual[Option[T], Option[U]] = derived

tests/neg/derive-eq.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ object Test extends App {
2020
val y: Triple[List[Two], One, Two] = ???
2121
val z: Triple[One, List[Two], One] = ???
2222
x == y // OK
23+
y == x // OK
2324
x == x // OK
2425
y == y // OK
2526

26-
y == x // error
2727
x == z // error
2828
z == y // error
2929
}

tests/neg/equality1.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,19 @@ object equality1 {
117117
(1, "a") == (1, "a", true) // error: cannot compare
118118
(1, "a", true, 't', 10L) == (1, "a", 1.5D, 't', 10L) // error: cannot compare
119119

120+
121+
val ns1 = List(1, 2, 3, 4, 5)
122+
val ns2 = List(1, 2, 3, 4, 5)
123+
ns1 == ns2
124+
125+
val ss = List("1", "2", "3", "4", "5")
126+
ns1 == ss // error: cannot compare
127+
128+
ns1 match {
129+
case n :: ns =>
130+
println(s"head: $n, tail: ${ns.mkString("[", ",", "]")}")
131+
case Nil =>
132+
println("empty")
133+
}
134+
120135
}

0 commit comments

Comments
 (0)