【发布时间】:2016-11-24 09:53:47
【问题描述】:
我正在尝试理解 Swift 中的泛型文化,所以我写了一个小例子。但它不编译。
错误:无法推断通用参数“P” 我做错了,我无法理解。
protocol Protocol_1 {
associatedtype T
}
protocol Protocol_A {}
struct SomeStruct_2: Protocol_A {}
struct SomeStruct_1: Protocol_1 {
typealias T = Protocol_A
}
let struct1 = SomeStruct_1()
testFunction(t: struct1) // *Generic parameter 'P' could not be inferred*
func testFunction<P: Protocol_1>(t: P) where P.T : Protocol_A {
}
【问题讨论】:
-
Protocols do not conform to themselves 在 Swift 中,所以你不能使用
Protocol_A作为符合Protocol_A的类型。