I have a case class Foo
defined below. I want to override the behavior of ==
in that, so that the last element (optBar
) is ignored in the comparison. Here is what I have tried and it seems to work.
case class Bar(i:Int)
case class Foo(i:Int, s:String, optBar:Option[Bar]) {
override def equals(o:Any) = o match {
case Foo(`i`, `s`, _) => true
case _ => false
}
override def hashCode = i.hashCode*997 ^ s.hashCode * 991
}
val b = Bar(1)
val f1 = Foo(1, "hi", Some(b))
val f2 = Foo(1, "hi", None)
f1 == f2 // true
What I want to know is if the method of creating hashCode
is correct. I got it from this link.