正如错误所指出的那样,问题是,您不能将具有“自我”或相关类型要求的协议用作实际类型,因为您会丢失有关这些要求的类型信息。在这种情况下,您将丢失
==实现参数的类型信息,因为
Equatable它们必须与一致类型(即
Self)具有相同的类型。
解决方案几乎总是建立一个类型的橡皮擦。在期望类型相等的情况下,如果它们的
id属性相等,则可以简单地
id将其存储起来并在
==实现中进行比较。
struct AnyVehicle : Equatable { static func ==(lhs: AnyVehicle, rhs: AnyVehicle) -> Bool { return lhs.id == rhs.id } let id : String init<T : Vehicle>(_ base: T) { id = base.id }}
(请注意,为了符合Swift命名约定,我将您的
ID属性重
id命名为)
但是,更通用的解决方案是在类型橡皮擦中存储一个函数,该函数可以在类型转换之后
Vehicle根据 其
==实现比较两个任意符合的实例,以确保它们与创建类型橡皮擦的具体类型相同。 。
struct AnyVehicle : Equatable { static func ==(lhs: AnyVehicle, rhs: AnyVehicle) -> Bool { // forward to both lhs's and rhs's _isEqual in order to determine equality. // the reason that both must be called is to preserve symmetry for when a // superclass is being compared with a subclass. // if you know you're always working with value types, you can omit one of them. return lhs._isEqual(rhs) || rhs._isEqual(lhs) } let base: Identifiable private let _isEqual: (_ to: AnyVehicle) -> Bool init<T : Vehicle>(_ base: T) { self.base = base _isEqual = { // attempt to cast the passed instance to the concrete type that // AnyVehicle was initialised with, returning the result of that // type's == implementation, or false otherwise. if let other =print(AnyVehicle(Car(id: "foo")) == AnyVehicle(Tractor(id: "foo"))) // falseprint(AnyVehicle(Car(id: "foo")) == AnyVehicle(Car(id: "bar"))) // falseprint(AnyVehicle(Car(id: "foo")) == AnyVehicle(Car(id: "foo"))) // truevar array = [AnyVehicle]()array.append(AnyVehicle(Car(id: "VW")))array.append(AnyVehicle(Car(id: "Porsche")))array.append(AnyVehicle(Tractor(id: "John Deere")))array.append(AnyVehicle(Tractor(id: "Steyr")))var op = Operator()// compiles fine as AnyVehicle conforms to Equatable.op.operationOnCollectionOfEquatables(array: array).base as? T { return base == other } else { return false } } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)