【发布时间】:2017-06-23 06:38:07
【问题描述】:
在 Swift 中,我有一个这样的协议:
protocol P {
associatedtype T
func f(val:T)
}
我想定义一个这样的类:
class B<X:P> {
}
然后在 B 类中使用associatedtypeT。
我试过了:
class B<X:P> {
var v:T // compiler says "Use of undeclared type"
init() {
}
}
我也试过这个:
class B<X:P, Y> {
typealias T = Y
var v:T
init() {
}
func g(val:X) {
val.f(val: v) // compiler says "Cannot invoke 'f' with an argument list of type '(val:Y)'
}
}
有什么建议吗?
【问题讨论】: