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

Howard Lovatt howard.lovatt at gmail.com
Sat Apr 16 17:12:30 CDT 2016


I would suggest enumerate only for types that are subscriptable and that it
be defined as their (index, value) set that iterates in the collection's
indices order. IE:

    for index in collection.indices {
        let value = collection[index]
        ...
    }

and

    for (index, value) in collection.enumerate {
        ...
    }

are equivalent.

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.

On Saturday, 16 April 2016, Brent Royal-Gordon <brent at architechies.com>
wrote:

> > I would suggest an alternative; changing Range so that it is indexed
> like an array, an Int from 0 to count - 1. Such that aRange[index] is
> defined as start + index * stride. This will eliminate the problems with
> enumerate. A Range's generic type would be constrained to be an Arithmetic
> type, see extended floating point proposal.
>
> That papers over the problem for Slice/Range specifically, but it doesn't
> fix it for types with non-integer indices, like Dictionary and Set.
> enumerate() is not meant to be used for the purpose to which it is usually
> put.



-- 
-- Howard.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160417/ba7691a1/attachment.html>


More information about the swift-evolution mailing list