[swift-evolution] [RFC] New collections model: collections advance indices

Dmitri Gribenko gribozavr at gmail.com
Wed Mar 2 01:05:01 CST 2016

On Tue, Mar 1, 2016 at 10:51 PM, Howard Lovatt <howard.lovatt at gmail.com> wrote:
> Sorry, unfortunately I can't see that it really helps. All that happens is
> that the client of the collection now holds the reference to the collection
> as well as the reference to the iterator. In the more traditional model the
> client holds a reference to the iterator which in turn holds a reference to
> the collection.

I don't think there is a problem with iterators holding references to
the collection.  Iterators are value types, so they have to hold a
strong reference to the underlying collection, to make it non-uniquely
referenced, to prevent it from being mutated in place.  It is a
feature that iterators traverse a snapshot of the collection --
because of value semantics.

// Append array to itself:
for x in myArray { // for loop is just syntactic sugar for extracting
an iterator, and consuming it.

I think you might be confusing iterators and indexes.  Iterators
traverse the collection once.  Once you moved the iterator to the next
position, you can't move it back.  Sequences and collections both have
iterators.  Indices, on the other hand, are the basis of the
multi-pass nature of the collection.  If you hold onto an index, you
can use it multiple times to refer to collection's element, and even
mutate it.


(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/

More information about the swift-evolution mailing list