I use Scala 3 with -language:strictEquality
enabled.
I am defining a new generic collection type, MyCol[T]
. I want ==
to be usable with instances of that collection type, so I add
given[T](using CanEqual[T, T]): CanEqual[MyCol[T], MyCol[S]] =
CanEqual.derived
This way, as long as equality is available for T
, it should also be available for MyCol[T]
. When equality is not available for T
, MyCol[T]
is still a valid type, but equality is not available for it either.
However, I’m stuck when trying to override equals
. In order to implement it, I need to check whether pairs of elements are equal. But, due to strict equality, I cannot compare two elements of type T
unless CanEqual[T, T]
is given…
override def equals(obj: Any): Boolean = obj match
case that: MyCol[_] =>
def elementEquals(element1: T, element2: T): Boolean = ???
How can I implement elementEquals
without always requiring using CanEqual[T, T]
for the construction of a MyCol
instance?