您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在实现Equatable的结构体上进行操作

在实现Equatable的结构体上进行操作

正如错误所指出的那样,问题是,您不能将具有“自我”或相关类型要求的协议用作实际类型,因为您会丢失有关这些要求的类型信息。在这种情况下,您将丢失==实现参数的类型信息,因为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 = $0.base as? T {
                return base == other
            } else {
                return false
            }
        }
    }
}
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)
其他 2022/1/1 18:13:42 有568人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶