Paul Young Paul Young - 14 days ago 8
Swift Question

How can I define an extension to CollectionType so that its methods are available to dictionaries?

I was recently able to change an extension from:

extension Array where Element: Encodable { ... }


to:

extension CollectionType where Generator.Element: Encodable { ... }


in order to later be able to apply type constraints with
CollectionType
and
Encodable
in a different extension.

Now I'm trying to do the same thing with
Dictionary
, changing:

extension Dictionary where Key: StringLiteralConvertible, Value: Encodable { ... }


to:

extension CollectionType where Generator.Element == (Key: StringLiteralConvertible, Value: Encodable) { ... }


However, the latter appears not to be a valid substitute for the
Dictionary
type, since now methods defined within the extension can't be called on dictionaries.

How can I define an extension to
CollectionType
(or maybe
SequenceType
) so that its methods are available to dictionaries?




Update

I wanted to note that I've tried adding 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'd prefer not to have to do this and keep the extensions to
Array
and
Dictionary
.

However, I also need to write extensions for the
Optional
variants. My understanding is that due to their use of generics, it's not possible to write a type constraint for
Optional
that constrains it to
Array
or
Dictionary
.

Answer

This works for my current use case:

extension CollectionType where Self: DictionaryLiteralConvertible, Self.Key: StringLiteralConvertible, Self.Value: Encodable, Generator.Element == (Self.Key, Self.Value) { ... }