SLC: add CanEqual for named tuples

the CanEqual typeclass is used to prevent nonsensical comparisons.

It already has an instance defined for Tuple, but not for Named Tuples.

e.g.

scala> (1,2,3) == (1,2)
-- [E172] Type Error: ----------------------------------------------------------
1 |(1,2,3) == (1,2)
  |^^^^^^^^^^^^^^^^
  |Values of types (Int, Int, Int) and (Int, Int) cannot be compared with == or !=.
  |I found:
  |
  |    Tuple.canEqualTuple[H1, T1, H2, T2](CanEqual.canEqualAny[H1, H2],
  |      Tuple.canEqualTuple[H1, T1, H2, T2](CanEqual.canEqualAny[H1, H2],
  |        /* missing */summon[CanEqual[T1, T2]])
  |    )
  |
  |But no implicit values were found that match type CanEqual[T1, T2].
1 error found

suspiciously allowed for named tuple equivalent:

scala> (a=1,b=2,c=3) == (d=1,e=2)
val res2: Boolean = false

I propose that the CanEqual instance for named tuples should also enforce that the names match, not just the field slots - following the types.

see PR Add CanEqual instance for NamedTuple by bishabosha · Pull Request #24890 · scala/scala3 · GitHub

12 Likes