Skip to content

Instantly share code, notes, and snippets.

@codelynx
Created March 6, 2015 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codelynx/cc24d1fead1d9ce779a1 to your computer and use it in GitHub Desktop.
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?
// 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