【发布时间】:2017-09-29 05:50:13
【问题描述】:
这是我的简化代码:
class MyClass {
func returnSomething(argument: Protocol2) {}
}
protocol Protocol2: Protocol1 where E == Int {
}
protocol Protocol1 {
associatedtype E
func doSomething(_ value: E)
}
编译器给我以下错误:Protocol 'Protocol2' can only be used as a generic constraint because it has Self or associated type requirements。
我知道需要先解析关联类型 E,然后才能将协议用作函数中的参数,但鉴于 Protocol2 提供了该信息,为什么我仍然无法编译该代码?
【问题讨论】:
-
我认为正确的语法是
func returnSomething<T: Protocol2>(argument: T)? -
@NicolasMiari 谢谢!