Swift - 与where子句的协议中的关联类型?

Dan*_*hin 6 associated-types swift swift2

考虑以下:

protocol SomeProtocol {
  typealias F: Foo
  typealias FB: FooBar where FB.Foo == F
}
Run Code Online (Sandbox Code Playgroud)

但这不编译,因为我们不能像这样放入where子句typealias.

我必须在这里遗漏一些东西,因为这可以很容易地做到type parameterization这样:

struct SomeStruct<F: Foo, FB: FooBar where FB.Foo == F> {}
Run Code Online (Sandbox Code Playgroud)

where条款相当于associated type什么?

Qby*_*yte 0

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) {
    ...
}
Run Code Online (Sandbox Code Playgroud)