Skip to content

Commit ddc96b9

Browse files
authored
Add CanEqual instance for Map (#15886)
Fixes #15849
2 parents f3f8ae6 + 27f6cac commit ddc96b9

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

library/src/scala/CanEqual.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package scala
22

33
import annotation.implicitNotFound
4-
import scala.collection.{Seq, Set}
4+
import scala.collection.{Seq, Set, Map}
55

66
/** A marker trait indicating that values of type `L` can be compared to values of type `R`. */
77
@implicitNotFound("Values of types ${L} and ${R} cannot be compared with == or !=")
@@ -26,14 +26,18 @@ object CanEqual {
2626
given canEqualNumber: CanEqual[Number, Number] = derived
2727
given canEqualString: CanEqual[String, String] = derived
2828

29-
// The next 6 definitions can go into the companion objects of their corresponding
29+
// The following 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
3232
given canEqualSeqs[T, U](using eq: CanEqual[T, U]): CanEqual[Seq[T], Seq[U]] = derived
3333
given canEqualSeq[T](using eq: CanEqual[T, T]): CanEqual[Seq[T], Seq[T]] = derived // for `case Nil` in pattern matching
3434

3535
given canEqualSet[T, U](using eq: CanEqual[T, U]): CanEqual[Set[T], Set[U]] = derived
3636

37+
given canEqualMap[K1, V1, K2, V2](
38+
using eqK: CanEqual[K1, K2], eqV: CanEqual[V1, V2]
39+
): CanEqual[Map[K1, V1], Map[K2, V2]] = derived
40+
3741
given canEqualOptions[T, U](using eq: CanEqual[T, U]): CanEqual[Option[T], Option[U]] = derived
3842
given canEqualOption[T](using eq: CanEqual[T, T]): CanEqual[Option[T], Option[T]] = derived // for `case None` in pattern matching
3943

tests/neg/equality1.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,9 @@ object equality1 {
132132
println("empty")
133133
}
134134

135+
Map("k1" -> 1) == Map("k2" -> 2, "k3" -> 3)
136+
Map(Color.Red -> Status.Inactive) == Map(Color.Green -> Status.Active(5))
137+
138+
Map("k1" -> 5) == Map('k' -> 5) // error
139+
Map("k1" -> new A) == Map("k2" -> new B) // error
135140
}

0 commit comments

Comments
 (0)