Swift - associated types in protocol with where clause?

放肆的年华 提交于 2019-12-22 05:37:26

问题


Consider the following:

protocol SomeProtocol {
  typealias F: Foo
  typealias FB: FooBar where FB.Foo == F
}

But this doesn't compile since we cannot put where clause in typealias like that.

I must be missing something here since this can be easily done with type parameterization like this:

struct SomeStruct<F: Foo, FB: FooBar where FB.Foo == F> {}

What's the where clause equivalent for associated type?


回答1:


Type parameterization of associated types in protocols is currently not supported in Swift (2.1).

Although in this case you don't even need the where clause for functionality. It's more the convenience you get where you can do this:

func someFunc<T: SomeProtocol>(someProt: T, foo: T.F) {
    ...
}

// Instead of this:

func someFunc<T: SomeProtocol>(someProt: T, foo: T.FB.Foo) {
    ...
}


来源:https://stackoverflow.com/questions/33871490/swift-associated-types-in-protocol-with-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!