问题
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