简体   繁体   English

在协议中声明之前,Swift 扩展计算变量无法正确读取

[英]Swift Extension computed variable not read correctly until declared in protocol

I have a protocol extension which declares and assigns a static computed variable:我有一个协议扩展,它声明并分配一个静态计算变量:

protocol DataType {
    
}
extension DataType {
    static var mocks: [Self] { [] }
}

Then I have another protocol named Provider which has an associatedtype requirement of the DataType protocol and an extension:然后我有另一个名为 Provider 的协议,它具有 DataType 协议和扩展的associatedtype类型要求:

protocol Provider {
    associatedtype Data: DataType
}

extension Provider {
    static var mock: [Data] {
        Data.mocks
    }
}

I then create the following types that conform to DataType and Provider:然后我创建了以下符合 DataType 和 Provider 的类型:

struct Car: DataType {
    var name: String
    
    static var mocks: [Car] {
        [.init(name: "Nissan"), .init(name: "Toyota")]
    }
}

struct CarProvider: Provider {
    typealias Data = Car
}

print(CarProvider.mock)

When I print this out (an empty array [] ), the CarProvider static variable mock prints out the default value of the mocks variable of DataType - even when Car has an assigned array value for mocks inside its struct definition当我打印出来(空数组[]CarProvider静态变量mock打印出的默认值mocks数据类型的变量-即使当Car具有用于分配的阵列值mocks其结构定义内

However, as soon as I declare the mocks property inside the DataType protocol as a requirement, then the mocks value of Car is correctly read (printing the correct values: [__lldb_expr_93.Car(name: "Nissan"), __lldb_expr_93.Car(name: "Toyota")] ):但是,一旦我将DataType协议中的mocks属性声明为要求,就会正确读取Carmocks值(打印正确的值: [__lldb_expr_93.Car(name: "Nissan"), __lldb_expr_93.Car(name: "Toyota")] ):

protocol DataType {
    static var mocks: [Self] { get }
}

Why is the property definition required in the Protocol definition in the first place?为什么首先需要在协议定义中定义属性? Shouldn't the extension value be sufficient?扩展值不应该足够吗? And since the Car struct is assigning its own value to the mocks variable, shouldn't that be read instead of the default extension value?并且由于Car结构将自己的值分配给mocks变量,难道不应该读取它而不是默认扩展值吗?

Why is the property definition required in the Protocol definition in the first place?为什么首先需要在协议定义中定义属性? Shouldn't the extension value be sufficient?扩展值不应该足够吗?

No. An implementation in a protocol extension can only be considered a true default implementation , if there is some way for it to be overridden in scope.不可以。协议扩展中的实现只能被视为真正的默认实现,前提是有某种方法可以在范围内覆盖它。 Without having the requirement in the protocol, Swift has no reason or mechanism to look beyond an extension, because there's nothing else that will match, semantically.没有协议中的要求,Swift 没有理由或机制超越扩展,因为在语义上没有其他匹配项。

protocol DataType { }

extension DataType {
  static var mocks: [Self] { [] }
}

func mocks<Data: DataType>(_: Data.Type) -> [Data] {
  Data.mocks // This *is* the extension. That is the only truth.
}
protocol DataType {
  static var mocks: [Self] { get }
}

extension DataType {
  static var mocks: [Self] { [] }
}

func mocks<Data: DataType>(_: Data.Type) -> [Data] {
  Data.mocks // Now, we can dispatch to a concrete `Data` type!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM