# equatable

## Implement Equatable protocol with Inheritance

To implement the equatable protocol is as simple as follow:

```swift
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.

```swift
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

```swift
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

* <https://forums.swift.org/t/implement-equatable-protocol-in-a-class-hierarchy/13844>
* <https://gist.github.com/fjfdeztoro/a5097f1b24379e127674eb1df8c97d96>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryanzzff.gitbook.io/til/swift/equatable.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
