I have a case class Pair(a: Int, b: Int)
, which represents a pair of 2 integers. In order to have Pair(2, 5) == Pair(5, 2)
, I overrided the equals
method as follows.
override def equals(that: Any): Boolean = that match {
case Corner(c, d) => (a == c && b == d) || (a == d && b == c)
case _ => false
}
Now the equality holds true, Pair(2, 5) == Pair(5, 2)
returns true, like I wanted. However, this does not work when pattern matching:
Pair(2, 5) match {
case Pair(5, 2) => print("This is what I want")
case _ => print("But this is what I get")
}
Could anyone please assist me? Can/should I even do it this way? What are the alternatives? I really don't want to write case Pair(2, 5) | case(5, 2) =>
every time I pattern match with pairs.