[英]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.