I recently managed to change the extension from:
extension Array where Element: Encodable { ... }
in
extension CollectionType where Generator.Element: Encodable { ... }
so that later you can apply type restrictions with CollectionType and Encodable in another extension.
Now I'm trying to do the same with Dictionary , changing:
extension Dictionary where Key: StringLiteralConvertible, Value: Encodable { ... }
in
extension CollectionType where Generator.Element == (Key: StringLiteralConvertible, Value: Encodable) { ... }
However, the latter does not seem to be a valid replacement for the Dictionary type, since now the methods defined in the extension cannot be called in dictionaries.
How to define an extension for CollectionType (or possibly SequenceType ) so that its methods are accessible for dictionaries?
Update
I wanted to notice that I tried to add type constraints as follows, without success:
extension CollectionType where Self: protocol<Indexable, SequenceType, DictionaryLiteralConvertible>, Self.Key: protocol<Hashable, StringLiteralConvertible>, Self.Value: Encodable, Self.Generator.Element == (Key: StringLiteralConvertible, Value: Encodable) { ... }
Update 2
Since user2194039 asked, I would rather not do this and save the extensions to Array and Dictionary .
However, I also need to write extensions for Optional options. I understand that due to the use of generics, it is not possible to write a type constraint for Optional that restricts it to Array or Dictionary .