[英]Swift protocols with mutually recursive associated type constraints in a function
Is it possible to constrain two generic parameters associated types in a function to each other?是否可以将 function 中的两个通用参数关联类型相互约束?
I'm trying to do something like this:我正在尝试做这样的事情:
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()
}
It fails with the errors:它失败并出现以下错误:
'first' is not a member type of 'O'
'second' is not a member type of 'T'
I was able to get this compile by removing one of the constraints on the function:通过删除 function 上的一个约束,我能够得到这个编译:
func f<O: One, T>(o: O) -> T where O.first == T, T.second == O {
fatalError()
}
You don't need to say that T
conforms to Two
because that's already implied by O.first == T
(because O.first
must conform to Two
).您不需要说
T
符合Two
,因为O.first == T
已经暗示了这一点(因为O.first
必须符合Two
)。 Once I took that out, this compiled.一旦我把它拿出来,这个就编译好了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.