lundi 20 avril 2015

Forcing all implementations of a trait to override equals

I have a trait for which I know that reference equality is never the correct implementation of equals. Implementations of the trait can be written by many users, and practice shows that sometimes they fail to override equals. Is there a way to require it?

In practice implementations are usually case classes, which override equals automatically, and we can approach requiring that by having Product as the self-type of the trait, however, I'd like to see a solution which allows non-case classes overriding equals as well.

One more approach I thought of while writing this question is to override equals in the trait to call an abstract method, but unfortunately, this doesn't work for case class implementations.

Aucun commentaire:

Enregistrer un commentaire