繁体   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