理想情况下,我会让服务器实现 Equatable 协议,但我遇到了问题。这是我的代码
protocol Server {
var ipAddress: String { get }
// simplified for this question
}
func ==<T:Server>(lhs: T, rhs: T) -> Bool {
return lhs.ipAddress == rhs.ipAddress
}
func !=<T:Server>(lhs: T, rhs: T) -> Bool {
return lhs.ipAddress != rhs.ipAddress
}
func ==<T:Server, U:Server>(lhs: T, rhs: U) -> Bool {
return lhs.ipAddress == rhs.ipAddress
}
func !=<T:Server, U:Server>(lhs: T, rhs: U) -> Bool {
return lhs.ipAddress != rhs.ipAddress
}
func doSomething(server0: Server, server1: Server) {
// I want to compare to Server objects
// !!! Compile Error !!!
// Binary operator '==' cannot be applied to two 'Server' operands
guard server0 == server1 else {
print("SAME BAD")
return
}
print("DO stuff")
}
最终,我只想将抽象协议对象与彼此进行比较。大多数其他示例都在比较具体类型。
我是因为尝试这个而疯狂还是什么?:P