因此,首先,我了解其中包含associatedtype
的协议将变得通用,因此将它们用作属性毫无意义。但是问题是,为什么已经受到限制的协议仍属于“成为通用”性质?
例如:
protocol ItemCollection {
associatedtype ItemType
var items: [ItemType]
}
protocol StringItemCollectable: ItemCollection where ItemType == String {
// other implementation
}
class Foo {
// item model
var itemCollectionModel: StringItemCollectable? // Compiler here is still complaining about using generic protocols
}
如果我创建一个符合StringItemCollectable
的具体类型,则编译器将知道items
是String
的数组。
struct TestCollectionModel: StringItemCollectable {
var items: [String] // this would be required and already known from the compiler
}
为什么编译器会认为class Foo
中受约束的协议仍然是通用的?