[swift-evolution] Eliminate inconsistencies among primary collection types

Xiaodi Wu xiaodi.wu at gmail.com
Fri Apr 29 15:01:38 CDT 2016


On Fri, Apr 29, 2016 at 2:49 PM, Nate Cook via swift-evolution <
swift-evolution at swift.org> wrote:

> > On Apr 29, 2016, at 1:57 PM, Wolfgang H. via swift-evolution <
> swift-evolution at swift.org> wrote:
> >
> > Inconsistency A. is that “Set” currently is the only collection type
> conforming to protocol 'Hashable'”. IMHO both “Array” and “Dictionary”
> should conform to protocol “Hashable”, too.
>
> Set is the only Hashable* collection type because it's the only collection
> type where all of its values are always Hashable themselves. Array<Element>
> can be an array of a non-Hashable type; Dictionary<Key: Value> can have a
> non-Hashable type for Value. If/when Swift gains conditional protocol
> compliance, arrays and dictionaries of Hashable types could themselves be
> Hashable, but not until then.
>
> > Inconsistency B. is that “Array<Int>” is typesafe if used as element of
> an “Array” but is non-typesafe if used as element of “Set” or “Dictionary”.
> The non-typesafety may result from the mapping of “Array<Int>” to “NSArray“.
>
> Since Array<Element> isn't Hashable, you can't create a Set<Array<Int>>.
> The type system therefore falls back to using the bridged Objective-C type
> for Array<Int>, which can provide a hash value based on object identity.
>
> Nate
>
>
> *For my spell checker's sake, could we rename Hashable to Washable? Thanks



Well put, Nate.
It will be a great day when conditional protocol compliance comes to Swift.
Until then, there's good reason why Array and Dictionary can't be washed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160429/c4334445/attachment.html>


More information about the swift-evolution mailing list