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

Dave Abrahams dabrahams at apple.com
Tue Apr 19 17:20:45 CDT 2016


on Fri Apr 15 2016, Brent Royal-Gordon <swift-evolution at swift.org> 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.

It also breaks an important “law of slicing,” that there is an index
correspondence between the slice and the outer collection.  That makes
it possible, e.g, to do

   let firstOne = c.index(of: something)
   let secondOne = c[firstOne..<c.endIndex].index(of: somethingElse)
   doSomethingWith(c[firstOne..<secondOne])

-- 
Dave



More information about the swift-evolution mailing list