I\'m trying to implement the ==
operator (from Equatable
) in a base class and its subclasses in Swift 3. All of the classes will only be used in Sw
Following the other answers I came up with this:
class Base : Equatable {
var x : Int
static func == (lhs: Base, rhs: Base) -> Bool {
return lhs.x == rhs.x
}
}
class Subclass : Base {
var y : String
static func == (lhs: Subclass, rhs: Subclass) -> Bool {
return lhs.y == rhs.y && (lhs as Base) == (rhs as Base)
}
}