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

Howard Lovatt howard.lovatt at gmail.com
Fri Apr 15 21:34:32 CDT 2016

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.

On Saturday, 16 April 2016, Andrew Bennett via swift-evolution <
swift-evolution at swift.org> wrote:

> I support a fixed version. I don't think that's mutually exclusive with
> removing enumerate.
> This thread seems to be a result of a misused API, so I think it's
> necessary to make it obvious when that API changes. I think we'd want a
> period of deprecation with both enumerate() and a new (hopefully more clear
> and concise) method, so that people can fix their code.
> I think this would be a good change:
> + @available(*, deprecated, message="it will be removed in Swift 3")
>   public func enumerate() -> EnumerateSequence<Self>
> + public func indexed() -> Zip2Sequence<Self.Index,Self> { return zip(self.indices, self) }
> On Sat, Apr 16, 2016 at 11:12 AM, Charles Srstka <cocoadev at charlessoft.com
> <javascript:_e(%7B%7D,'cvml','cocoadev at charlessoft.com');>> wrote:
>> On Apr 15, 2016, at 8:09 PM, Andrew Bennett via swift-evolution <
>> swift-evolution at swift.org
>> <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>> wrote:
>> I'm in support of a method of getting a sequence of (Index,Value) pairs
>> (as you know from the other thread). I think I'm leaning toward option
>> three if it's equivalent to `zip(self.indices, self)`, ideally as a concise
>> property like keys/values on a Dictionary.
>> I've been using zip in production. I presumed enumerate was intended for
>> enumerating indices, and that it had just been forgotten when the slice
>> changes went through. I'm in favour of removing it.
>> Instead of removing it, how about just fixing it so that it returns the
>> proper indexes rather than arbitrary numbers?
>> Charles

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

More information about the swift-evolution mailing list