Swift-协议中与where子句相关的类型?
问题描述:
请考虑以下内容:
protocol SomeProtocol {
typealias F: Foo
typealias FB: FooBar where FB.Foo == F
}
但是这不能编译,因为我们不能像这样将where
子句放在typealias
中.
But this doesn't compile since we cannot put where
clause in typealias
like that.
我必须在这里丢失一些东西,因为可以使用type parameterization
这样轻松地做到这一点:
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> {}
associated type
的where
子句是什么?
答
Swift(2.1)当前不支持协议中关联类型的类型参数化.
Type parameterization of associated types in protocols is currently not supported in Swift (2.1).
尽管在这种情况下,您甚至不需要where子句来实现功能.您可以从中获得更多的方便:
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) {
...
}