Skip to content

Commit 4931e71

Browse files
committed
Add CanEqual instance for Seq to match Nil case
1 parent 58d1b10 commit 4931e71

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

library/src/scala/CanEqual.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ object CanEqual {
2929
// The next three definitions can go into the companion objects of classes
3030
// Seq and Set. 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
34+
3335
given canEqualSet[T, U](using eq: CanEqual[T, U]): CanEqual[Set[T], Set[U]] = derived
3436
}

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
@@ -3,4 +3,19 @@ object equality1 {
33
class A
44
class B
55
new A == new B // error: cannot compare
6+
7+
val ns1 = List(1, 2, 3, 4, 5)
8+
val ns2 = List(1, 2, 3, 4, 5)
9+
ns1 == ns2
10+
11+
val ss = List("1", "2", "3", "4", "5")
12+
ns1 == ss // error: cannot compare
13+
14+
ns1 match {
15+
case n :: ns =>
16+
println(s"head: $n, tail: ${ns.mkString("[", ",", "]")}")
17+
case Nil =>
18+
println("empty")
19+
}
20+
621
}

0 commit comments

Comments
 (0)