8

On migrating our code to Scala 2.9 we've found large swathes of it that didn't work and failed silently. We tracked it down to case classes that extend Proxy not being equal. In our code we don't extend Proxy directly, we just extend classes in libraries that extend Proxy.

Any help would be greatly appreciated.

In 2.8

scala> case class Test(a:String) extends Proxy {
     |   def self = a
     | }
defined class Test

scala> 

scala> val label = new Test("bla")
label: Test = bla

scala> println(label == label) // this is TRUE
true

scala> println(label == "bla")
true

In 2.9

scala> case class Test(a:String) extends Proxy {
     |   def self = a
     | }
defined class Test

scala> 

scala> val label = new Test("bla")
label: Test = bla

scala> println(label == label) // this is now FALSE
false

scala> println(label == "bla")
true

Update

I think this can only be a bug in Scala 2.9. Otherwise if you have a case class that extends any other class you have to investigate that base class's hierarchy to make sure at no point is it extending Proxy. We won't be able to do this in our code, we'll just be able to fix the more obvious bugs. If this is intended behaviour then a compiler warning is a must. Does that sound about right?

Update

Also being discussed on the scala mailing list.

Update

I've filed a bug

David
  • 1,862
  • 2
  • 22
  • 35
  • 1
    Did you end up reporting this as a bug? (If so, link?) The documentation for Proxy does warn you that it may lead to equals not being symmetric, but it doesn't warn you equals might not even be reflexive...! – Seth Tisue Jul 15 '11 at 11:19
  • 1
    Seth, I agree, this can only really be a bug. I've updated with a link above. – David Jul 15 '11 at 16:27
  • The bug was reported fixed on 10/Sep/11, fyi. – James Moore Oct 29 '11 at 05:33
  • Yeah but I'm waiting for it to be assigned to a version of Scala before I celebrate. It's been fixed a while but has no sign of being released yet. – David Oct 31 '11 at 10:38

3 Answers3

6

In 2.9 they changed the equals method from:

override def equals(that: Any): Boolean = 
  if(that == null) false 
  else that equals self

to

override def equals(that: Any): Boolean = that match {
 case null       => false
 case x: Equals  => (x canEqual self) && (x equals self)
 case x          => (x equals self)
}

x: Equals doesn't equal self for some reason.

You can override the equals method to fix it.

Joe0
  • 153
  • 8
  • 1
    I think the easiest fix is just to change the case class to a class for us. However this seems broken, or at least a behaviour change that should have been documented. Thanks – David Jul 13 '11 at 07:43
  • 1
    I think this is probably a bug, but this answer explains where the bug originated so I'll accept it. Thanks Joe. – David Jul 15 '11 at 16:47
1

This will solve your problem

case class Test(a: String) extends Proxy {
   def self = a
   def canEqual(that: Any) = that match {
      case that: String => true
      case _ => false
   }
}
agilesteel
  • 16,775
  • 6
  • 44
  • 55
0

So why don't you overwrite the equals method? That should solve the problem.

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
  • 1
    By using a case class you should be getting an equals method that works. Using a case class and overriding the equals method seems a bit crazy, I could just not use a case class, which would work. It's just our code is full of them. – David Jul 13 '11 at 07:37
  • Sure this is not ideal, but I don't see another way. – Kim Stebel Jul 13 '11 at 08:12