[swift-evolution] [Idea] Replace enumerate() with something more explicit

Dave Abrahams dabrahams at apple.com
Tue Apr 19 17:24:41 CDT 2016


on Sun Apr 17 2016, Howard Lovatt <swift-evolution at swift.org> wrote:

> Yes I am proposing something like protocol Subscriptable that defines the
> subscript operation and indices and entries properties.

That sounds like a protocol that is just a bag of syntax without
semantic requirements, which—forgive my pretentious tone—goes against
the principles of generic programming on which the standard library was
founded.  We won't have a “plusable” for things you can apply the “+”
operator to, for similar reasons.

> On Sunday, 17 April 2016, Brent Royal-Gordon
> <brent at architechies.com> wrote:
>
>     > With the above definition I would suggest a name change to entries, since
>     a Dictionary's keys are not necessarily numbers, hence enumerate is
>     misleading.
>     >
>     > Nothing for Set since it isn't subscriptable.
>
>     I think you're slightly confused. All Collections have an Index.
>     Dictionary's Index is not its Key; it is an opaque type which references an
>     entry in its internal table. Set also has an Index; again, it is an opaque
>     type which references an entry in its internal table. Your `enumerate()` (or
>     my `indexed()`) would return these opaque `Index`es on all of these types.
>
>     If you want Array to return `(Int, Element)`, Dictionary to return `(Key,
>     Value)`, and Set to not have the operation at all, you're describing
>     something ad-hoc and entirely disconnected from the Collection type.
>
>     --
>     Brent Royal-Gordon
>     Architechies

-- 
Dave



More information about the swift-evolution mailing list