[swift-evolution] Removing enumerated?

Haravikk swift-evolution at haravikk.me
Fri Feb 3 17:52:04 CST 2017


I'm in favour of getting rid of enumerated; I think like many people I used it expecting to get actual indices, and it's very easy to think this when working with arrays (as the values will in fact be perfectly valid). In reality the right way to do it is with one of the following:

for eachIndex in myArray.indices { print("\(eachIndex): " + myArray[eachIndex]) }
for eachIndex in myArray.startIndex ..< myArray.endIndex { print("\(eachIndex): " + myArray[eachIndex]) }
var offset = 0; for eachEntry in myArray { print("\(offset): \(eachEntry)"); offset += 1 }

However, I think I'd still support the inclusion of an enumerating and an indexing type that sequences/collections can be wrapped in to perform enumeration/indexing if you still want it.

> On 31 Jan 2017, at 14:24, Chris Eidhof via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hey everyone,
> 
> I've organized a number of Swift workshops over the last two years. There are a couple of things that keep coming up, and a couple of mistakes that I see people making over and over again. One of them is that in almost every workshop, there's someone who thinks that `enumerated()` returns a list of (index, element) pairs. This is only true for arrays. It breaks when using array slices, or any other kind of collection. In our workshops, I sometimes see people doing something like `x.reversed().enumerated()`, where `x` is an array, and somehow it produces behavior they don't understand.
> 
> A few ways I think this could be improved:
> 
> - Move enumerated to Array
> - Change enumerated to return `(Index, Iterator.Element)` (this would mean we at least need to move it to collection)
> - Remove enumerated
> - Keep things as is
> 
> In any case, just wanted to share my experience (gained from teaching people). 
> 
> -- 
> Chris Eidhof
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170203/8bf53301/attachment.html>


More information about the swift-evolution mailing list