![](/img/trans.png)
[英]Access associated type of a custom protocol in a where clause on generic types in Swift
[英]Swift - associated types in protocol with where clause?
考虑以下:
protocol SomeProtocol {
typealias F: Foo
typealias FB: FooBar where FB.Foo == F
}
但是这不能编译,因为我们不能把where
子句放在typealias
中。
我必须在这里遗漏一些东西,因为这可以通过type parameterization
轻松完成,如下所示:
struct SomeStruct<F: Foo, FB: FooBar where FB.Foo == F> {}
什么是associated type
的where
子句等效 ?
Swift(2.1)目前不支持协议中关联类型的类型参数化。
虽然在这种情况下你甚至不需要where子句来实现功能。 您可以通过以下方式获得更多便利:
func someFunc<T: SomeProtocol>(someProt: T, foo: T.F) {
...
}
// Instead of this:
func someFunc<T: SomeProtocol>(someProt: T, foo: T.FB.Foo) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.