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 typeclass instance for Seq to match Nil case #13265

Merged
merged 1 commit into from
Aug 23, 2021
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
6 changes: 4 additions & 2 deletions library/src/scala/CanEqual.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines -32 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the original given isn't enough to match Nil /cc @odersky

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same logic as the Option/Either PR, you get:

    case Nil =>
         ^^^
Values of types object scala.collection.immutable.Nil and List[Int] cannot be compared with == or !=.
I found:

    CanEqual.canEqualSeq[Nothing, Int](/* missing */summon[CanEqual[Nothing, Int]])

But no implicit values were found that match type CanEqual[Nothing, Int].


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
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/derive-eq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
dwijnand marked this conversation as resolved.
Show resolved Hide resolved
x == x // OK
y == y // OK

y == x // error
x == z // error
z == y // error
}
15 changes: 15 additions & 0 deletions tests/neg/equality1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

}