swift: 为什么在重载“==”运算符时需要符合equatable?

swift: why need to conform equatable when overload the "==" operator?

我正在学习 swift 并阅读了有关扩展中运算符重载的主题,它喜欢:

extension StreetAddress: Equatable {
    static func == (lhs: StreetAddress, rhs: StreetAddress) -> Bool {
        return
            lhs.number == rhs.number &&
            lhs.street == rhs.street &&
            lhs.unit == rhs.unit
    }
}

但是我怎么知道我需要采用 Equatable?

我试图删除该协议,但功能仍然有效。 不会报告任何警告或错误。 为什么?

引用 Apple documentation:

To adopt the Equatable protocol, implement the equal-to operator (==) as a static method of your type

所以实现 Equatable 意味着你 必须 重载 == 运算符,因此这是一个构建错误:

extension StreetAddress: Equatable {
}

重载 == 运算符不需要并且与 Equatable 严格 无关,例如:

class StreetAddress {
    var theAddress:String?

    static func == (lhs: StreetAddress, rhs: StreetAddress) -> Bool {
        return lhs.theAddress?.lowercased() == rhs.theAddress?.lowercased()
    }
}