Can we get rid of cooperative equality?

I have the impression the discussion got derailed. I did not propose 1 != 1L and in fact would strongly object to this. To show why co-operative equality is irregular even if it looks regular at first, let me simplify the question to some synthetic classes ANY, A, and B with a === method:

  class ANY {
    def ===(that: ANY) = this eq that
  }

  case class A(x: String) extends ANY {
    def ===(that: A) = this.x == that.x
    def ===(that: B) = this.x == that.x
  }

  case class B(x: String) extends ANY {
    def ===(that: A) = this.x == that.x
    def ===(that: B) = this.x == that.x
  }

  val a = A("")
  val b = B("")

  a == b   // --> true
  (a: ANY) == (b: ANY)  // --> false

That’s what we would expect from Scala’s behavior. The point is, === is an overloaded method so the static types on which it is called matter. It also means that boxing is visible because the static types change. If === was a multi-method it would give true also for the second time, but Scala does not have multi-methods. On the other hand, for ==, we treat it as if it was a multi-method, or, rather, as if it had an extra-ordinarily complex and expensive implementation which makes it look like it is a multi-method for some types, but not for others, where we still use the overloaded behavior. This is what’s irregular about it.

1 Like