我正在尝试编写以下功能
func take<C: Collection where C.Iterator.Element Int>(_ value: C) {
print(value.first)
}
Run Code Online (Sandbox Code Playgroud)
但是我一直收到编译器错误:
'where' clause next to generic parameters is obsolete, must be written following the declaration's type
Run Code Online (Sandbox Code Playgroud)
您似乎对通用约束的语法不太熟悉。应该这样写:
func take<C: Collection>(_ value: C) where C.Iterator.Element == Int {
print(value.first)
}
Run Code Online (Sandbox Code Playgroud)
在此错误消息是有点混乱。基本上是要求您在之前写约束{
。