簡體   English   中英

Swift - 與where子句的協議中的關聯類型?

[英]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 typewhere子句等效

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM