equatable

Implement Equatable protocol with Inheritance

To implement the equatable protocol is as simple as follow:

class SimpleClass: Equatable {
    var propA: String?
    var propB: Int?
    public static func == (lhs: SimpleClass, rhs: SimpleClass) -> Bool {
        return lhs.propA == rhs.propA
            && lhs.propB == rhs.propB
    }
}

But the problem is, if you have an subclass, it is not going to work.

class SimpleSubClass: SimpleClass {
    var propC: String?

    public static func == (lhs: SimpleSubClass, rhs: SimpleSubClass) -> Bool {
        return lhs.propC == rhs.propC
    }
}

Because when you comparing two SimpleSubClass objects, only the super class's == function is called.

Solution

Add an extra equals function that can be overriden in subclass

class SimpleClass: Equatable {
    var propA: String?
    var propB: Int?
    public static func == (lhs: SimpleClass, rhs: SimpleClass) -> Bool {
        return lhs.equals(rhs: rhs)
    }

    func equals(rhs: SimpleClass) -> Bool {
        return self.propA == rhs.propA
            && self.propB == rhs.propB
    }
}

class SimpleSubClass: SimpleClass {
    var propC: String?

    public override func equals(rhs: SimpleClass) -> Bool {
        guard let other = rhs as? SimpleSubClass else { return false }
        return super.equals(rhs: other)
            && self.propC == other.propC
    }
}

References

Last updated