【发布时间】:2019-09-17 14:37:49
【问题描述】:
我有两个协议,每个协议都定义了一个关联类型。其中一个协议需要定义另一个协议类型的变量,其中它们都具有相同类型的关联类型。是否有可能以某种方式推断关联类型的类型?
protocol A {
associatedtype AModel
var b: B { get }
}
protocol B {
associatedtype BModel
func doAnother(anotherModel: BModel)
}
这是我尝试但没有成功的方法
protocol A {
associatedtype AModel
associatedtype TypedB = B where B.BModel == AModel
var another: TypedB { get }
}
protocol B {
associatedtype BModel
func doAnother(anotherModel: BModel)
}
【问题讨论】:
-
之所以有效,是因为
protocol Superfighter {}协议之一没有关联类型