Created
September 27, 2013 05:23
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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