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

时间:2015-11-23 12:49:00

标签: swift swift2 associated-types

请考虑以下事项:

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> {}

where等同于associated type Got in: 2.04 seconds string (20) "soidA321esa q alO321" 条款是什么?

1 个答案:

答案 0 :(得分: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) {
    ...
}