I have a hierarchy of 2 Swift classes and need them to implement Comparable:
class Thing : Comparable {
var name = "Thing"
init(name : String){
self.name = name
}
}
class Thingy: Thing {
override init(name: String){
super.init(name: "Thingy")
}
}
func ==(lhs: Thing, rhs:Thing)->Bool{
return lhs.name == rhs.name
}
To comply with Comparable, I need to implement this function:
func <(lhs: Thing, rhs: Thing) -> Bool{
return lhs.name < rhs.name
}
So far so good, but what happens if I need a specific < function for different subtypes, such as:
func <(lhs: SubThing, rhs: Thing) -> Bool{
return lhs.name < rhs.name
}
How am I supposed to do this? The compiler seems to ignore this last declaration.
Will it also work if the types are inverted?
lhs: SubThing, rhs: Thing
instead of
lhs: Thing, rhs: SubThing