Search code examples
scalamanifestequalstype-erasure

Scala: Problems with erasure on overriding equals function for parametrized classes


I'm having troubles on understanding well how to use manifests.

That's my problem: I've creat a new parametrized class C and tryed to override equals like this:

override def equals(that:Any)=that match{
 case that:C[T] => true /*do smth else not relevant*/
 case _ => false
}

Of course I recieve the "warning: non variable type-argument T in type pattern C[T] is unchecked since it is eliminated by erasure". I tryied so using manifests like I was using in many other functions:

override def equals(that:Any)(implicit manifest:Manifest[T])=that match{
 case that:C[T] => true
 case _ => false
}

But I recieved the "error: method equals overrides nothing" message.

I don't know how to fix this. Could anyone please help me?


Solution

  • You can't fix it. Welcome to the joys of smooth interoperation with java. The only way to improve equals from def equals(x: Any): Boolean is to write a different method.

    I'm always trying to convince martin that we should implement == desugaring differently, aiming at something like "def decentEquals[T](x: T)(implicit equiv: Equiv[T])" with default implicits and bridge methods to make it seamless unless you care, but he thinks equality tests shouldn't get any slower.