对实现Equatable的结构数组的操作

Mar*_*bar 9 arrays struct swift swift3

我有一系列不同的结构,所有实现Equatable协议,并试图将其传递给期望集合的函数where T.Iterator.Element: Equatable.我知道如何通过使用类来解决这个问题,只需创建一个class Vehicle: Identifiable, Equatable,然后制作CarTractor实现Vehicle.但是,我想知道使用结构和协议是否可行?

这是我正在尝试做的一个人为的例子

//: Playground - noun: a place where people can play

protocol Identifiable {
    var ID: String { get set }
    init(ID: String)
    init()
}

extension Identifiable {
    init(ID: String) {
        self.init()
        self.ID = ID
    }
}

typealias Vehicle = Identifiable & Equatable

struct Car: Vehicle {
    var ID: String

    init() {
        ID = ""
    }

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

struct Tractor: Vehicle {
    var ID: String

    init() {
        ID = ""
    }

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

class Operator {
    func operationOnCollectionOfEquatables<T: Collection>(array: T) where T.Iterator.Element: Equatable {
    }
}

var array = [Vehicle]() //Protocol 'Equatable' can only be used as a generic constraint because Self or associated type requirements

array.append(Car(ID:"VW"))
array.append(Car(ID:"Porsche"))
array.append(Tractor(ID:"John Deere"))
array.append(Tractor(ID:"Steyr"))

var op = Operator()
op.operationOnCollectionOfEquatables(array: array) //Generic parameter 'T' could not be inferred
Run Code Online (Sandbox Code Playgroud)

Ham*_*ish 8

问题是,正如错误所说,您不能将具有Self或相关类型要求的协议用作实际类型 - 因为您丢失了这些要求的类型信息.在这种情况下,您将丢失==实现参数的类型信息- 因为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
    }
}
Run Code Online (Sandbox Code Playgroud)

(请注意,我将您的ID属性重id命名为符合Swift命名约定)

但是,更通用的解决方案是在类型擦除器中存储一个函数,它可以在类型转换后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 = $0.base as? T {
                return base == other
            } else {
                return false
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

print(AnyVehicle(Car(id: "foo")) == AnyVehicle(Tractor(id: "foo"))) // false
print(AnyVehicle(Car(id: "foo")) == AnyVehicle(Car(id: "bar"))) // false
print(AnyVehicle(Car(id: "foo")) == AnyVehicle(Car(id: "foo"))) // true

var 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) 
Run Code Online (Sandbox Code Playgroud)