Reputation: 6114
I can do this:
extension Dictionary : MyProtocol {}
and I can do this:
extension Dictionary where Key: String, Value: Int {}
but I can't do this:
extension Dictionary where Key: String, Value: Int : MyProtocol {}
or this:
extension Dictionary : MyProtocol where Key: String, Value: Int {}
What's the correct way to format that to allow me to specify types and also specify adherence to a protocol?
Upvotes: 0
Views: 79
Reputation: 81
Answered here: How to declare Dictionary<String, Decimal> complies to protocol
TLDR: this is not possible.
It is planned for a future version of Swift: https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#conditional-conformances-
much more detail on how it will be implemented: https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md
Upvotes: 1
Reputation: 6114
OK, I'm an idiot. Just realised the last example gives this error:
Extension of type 'Dictionary' with constraints cannot have an inheritance clause
So presumably it simply isn't possible yet.
Upvotes: 0