Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CanEqual instance for Map #15886

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions library/src/scala/CanEqual.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scala

import annotation.implicitNotFound
import scala.collection.{Seq, Set}
import scala.collection.{Seq, Set, Map}

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

// The next 6 definitions can go into the companion objects of their corresponding
// The following 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 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 canEqualMap[K1, V1, K2, V2](
using eqK: CanEqual[K1, K2], eqV: CanEqual[V1, V2]
): CanEqual[Map[K1, V1], Map[K2, V2]] = derived

given canEqualOptions[T, U](using eq: CanEqual[T, U]): CanEqual[Option[T], Option[U]] = derived
given canEqualOption[T](using eq: CanEqual[T, T]): CanEqual[Option[T], Option[T]] = derived // for `case None` in pattern matching

Expand Down
5 changes: 5 additions & 0 deletions tests/neg/equality1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,9 @@ object equality1 {
println("empty")
}

Map("k1" -> 1) == Map("k2" -> 2, "k3" -> 3)
Map(Color.Red -> Status.Inactive) == Map(Color.Green -> Status.Active(5))

Map("k1" -> 5) == Map('k' -> 5) // error
Map("k1" -> new A) == Map("k2" -> new B) // error
}