I'm trying to make a method that will match on reference equality for any type, including primitives. How best to do this?
eq is only defined on AnyRef. If we try
def refEquals[A <% AnyRef, B <% AnyRef](a: A, b: B) = a eq b then on running refEquals(1,2) we find there are implicit methods in Predef including int2IntegerConflict to scupper such conversions.
I tried this:
def refEquals(a: Any, b: Any) = a match { case x: AnyRef => b match { case y: AnyRef => x eq y case _ => false } case x: Any => b match { case y: AnyRef => false case y: Any => x == y } } But this doesn't work (refEquals(1.0, 1.0) gives false) for reasons given by Rex Kerr here: Strange pattern matching behaviour with AnyRef
So how do we implement such a method?
edit: should have said "reference equality for reference types, or value equality for primitive types".
edit: here's the method using the idea from Rex's answer, for anyone who needs this and doesn't like typing:
def refEquals(a: Any, b: Any) = a match { case x: Boolean if b.isInstanceOf[Boolean] => x == b case x: Byte if b.isInstanceOf[Byte] => x == b case x: Short if b.isInstanceOf[Short] => x == b case x: Char if b.isInstanceOf[Char] => x == b case x: Int if b.isInstanceOf[Int] => x == b case x: Float if b.isInstanceOf[Float] => x == b case x: Double if b.isInstanceOf[Double] => x == b case x: Long if b.isInstanceOf[Long] => x == b case _ => a.asInstanceOf[AnyRef] eq b.asInstanceOf[AnyRef] }