我有一个需求,其中我有多个符合protocolP的struct
,稍后我通过Computer
struct 收集并执行像compute()
这样的方法,这有助于创建每个符合类型的实例
为什么以下代码不能运行,需要进行哪些更改才能运行:
import Foundation
protocol P {
static func instance() -> Self
}
struct A: P {
static func instance() -> A {
A()
}
}
struct B: P {
static func instance() -> B {
B()
}
}
struct Computer<T: P> {
static func compute() -> T {
T.instance()
}
}
let someArray: [P.Type] = [
A.self,
B.self
]
for value in someArray {
Computer<value>.compute() // ERROR: Cannot find type 'value' in scope
}