3
這是一個孤立的例子,所以可能看起來不太有用,但我仍然想知道爲什麼它不起作用?任何洞察力非常感謝。斯威夫特泛型(型號替代?)
protocol Prot: class {
init()
}
class A: Prot {
required init(){ }
}
struct Client<T: Prot> {
let tau: T.Type
}
if let aTau = A.self as? Prot.Type {
print(aTau === A.self) // ✅
Client(tau: A.self) // ✅
Client(tau: aTau) // ❌
}
錯誤是:
Cannot invoke initializer for type 'Client<_>' with an argument list of type '(tau: Prot.Type)'
這是[協議不符合自身?](http://stackoverflow.com/questions/33112559/protocol-doesnt-conform-to-itself)的變體 - 您不能使用'Prot'作爲符合'Prot'的類型。 – Hamish