Defining new collections with strict equality

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?

I just found the solution (duh):

def elementEquals(element1: T, element2: T): Boolean =
  given CanEqual[T, T] = CanEqual.derived
  element1 == element2

Probably a noob question, but is the purpose of overriding equals to allow for situations where there is no given CanEqual[T, T] in scope?

I want to override equals for MyCol, so that it doesn’t use object address equality that it would otherwise inherit from Object.

I do not want to override equals for T.

CanEqual[A, B] only specifies when (a: A) == (b: B) is legal, regardless of whether A or B overrides equals.