Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Created September 27, 2013 05:23
Show Gist options
  • Save tpolecat/6724500 to your computer and use it in GitHub Desktop.
Save tpolecat/6724500 to your computer and use it in GitHub Desktop.
How can you do this consistently? I don't think it is possible with universal equality.
class Animal(val name: String) {
override def equals(a: Any): Boolean =
a match {
case a: Animal if a.name == name => true
case _ => false
}
}
class Cat(name: String, val color: String) extends Animal(name) {
override def equals(a: Any): Boolean =
a match {
case c: Cat if super.equals(c) && c.color == color => true
case _ => false
}
}
val a: Animal = new Animal("Bob")
val b: Animal = new Animal("Bob")
val c: Animal = new Cat("Bob", "Orange")
a == b // true
b == a // true
a == c // true
c == a // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment