Equality of objects

Hi,

I’m trying to encode a set of different types of formulae and have the following equality method for True (and something similar for False) (this doesn’t deal with semantic equivalence but that’s ok for me):
However, when I test it, I get a SO error. I think because the pattern matching is calling the equals method itself.
What’s the correct way of implementing this idea that prevents this problem please?

object True extends Formula {
  override def equals(other: Any) = other match {
    case True => true
    case _ => false
  } 
  ...
}

>: True equals True creates the error.

The following should work.

object True extends Formula {
  override def equals(other: Any) = other match {
    case _ : True.type => true
    case _ => false
  } 
  ...
}

regards,
Siddhartha

1 Like

objects are singletons, they already have the semantics you are attempting to encode:

object True
object False

val reallyTrue = True
True == True //true
True == reallyTrue //true
True == False //false
False == False //true
1 Like

Thanks both. I presume that implies there is no need to override hashCode either.

1 Like

That’s correct