[英]Swift Protocols with Associated Type and Generics calling wrong function
[英]Swift protocols with mutually recursive associated type constraints in a function
是否可以将 function 中的两个通用参数关联类型相互约束?
我正在尝试做这样的事情:
protocol One {
associatedtype first: Two
}
protocol Two {
associatedtype second: One
}
func f<O: One, T: Two>(o: O) -> T where O.first == T, T.second == O {
fatalError()
}
它失败并出现以下错误:
'first' is not a member type of 'O'
'second' is not a member type of 'T'
通过删除 function 上的一个约束,我能够得到这个编译:
func f<O: One, T>(o: O) -> T where O.first == T, T.second == O {
fatalError()
}
您不需要说T
符合Two
,因为O.first == T
已经暗示了这一点(因为O.first
必须符合Two
)。 一旦我把它拿出来,这个就编译好了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.