Created
March 6, 2015 17:26
-
-
Save codelynx/cc24d1fead1d9ce779a1 to your computer and use it in GitHub Desktop.
Testing Equatable function between base class and its subclass but != seems not working as expected?
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
// Creature | |
class Creature : Equatable { | |
} | |
func ==(lhs: Creature, rhs: Creature) -> Bool { | |
return lhs === rhs | |
} | |
// Monster | |
class Monster : Creature { | |
let name: String | |
init(name: String) { | |
self.name = name | |
} | |
} | |
func ==(lhs: Monster, rhs: Monster) -> Bool { | |
return lhs.name == rhs.name | |
} | |
// (1) | |
//func !=(lhs: Monster, rhs: Monster) -> Bool { | |
// return lhs.name != rhs.name | |
//} | |
let a = Creature() | |
let b = Creature() | |
let elmo1 = Monster(name: "Elmo") | |
let elmo2 = Monster(name: "Elmo") | |
let oscar = Monster(name: "Oscar") | |
let telly = Monster(name: "Telly") | |
a == b | |
a == a | |
a == oscar | |
a != oscar | |
elmo1 != elmo1 | |
elmo1 != elmo2 // should be false without (1) ?? | |
elmo1 == elmo1 | |
elmo1 == elmo2 | |
oscar == telly | |
oscar != telly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment