<div dir="ltr">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.<div><br></div><div>Maybe the problem arose with the double indirection trick. Would a better approach be to keep it simple:</div><div><br></div><div> struct SomeCollection<E>: Collection {</div><div> private var refToInternalStorage: UnsafePointerOfSomeSort<E></div><div> func iterable() -> SomeCollectionIterator { return SomeCollectionIterator(self) }</div><div> mutating func append(element: E) {</div><div> copyInternalStorageIfNecessary(size + 1) // Makes a copy if either not large enough or if already aliased</div><div> // Append `element` to `refToInternalStorage`</div><div> }</div><div> // Other functions access `refToInternalStorage`, but if mutating call `copyInternalStorageIfNecessary(Int)` before modifying</div><div> }</div><div><br></div><div> struct SomeCollectionIterator<E>: Iterator {</div><div> private let someCollection: SomeCollection<E> // Note let not var - could be direct link to internal storage</div><div> private var index: Index</div><div> init(someCollection: someCollection <E>) { </div><div> self.someCollection = someCollection </div><div> index = someCollection.firstIndex()</div><div> }</div><div> // next()</div><div> }</div><div><br></div><div>Obviously above is pseudo code - but hopefully you get the idea.</div><div><br></div><div>The difference comes when with the proposal you do:</div><div><br></div><div> var array = [1]</div><div> var iterator = array.makeIterator()</div><div> array.removeAll()</div><div> array.next(iterator) // Runtime exception</div><div><br></div><div>Similarly with modifications in another thread. With the above suggested `obvious` implementation:</div><div><br></div><div> var array = [1]</div><div> var iterator = array.iterator()</div><div> array.removeAll()</div><div> iterator.next() // OK, returns 1 because it has the original before array was mutated.</div><div><br></div><div>In essence I am saying do the obvious, since that is easiest for the clients (i.e. the programmers that use Swift).</div><div><br></div><div>Sorry to be negative,</div><div><br></div><div> -- Howard.</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 2 March 2016 at 14:03, Trent Nadeau via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Ah. Thanks. I've read through that proposal before but totally forgot that change.</div><div class="gmail_extra"><span class=""><br><div class="gmail_quote">On Tue, Mar 1, 2016 at 10:01 PM, Dmitri Gribenko <span dir="ltr"><<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Tue, Mar 1, 2016 at 6:55 PM, Trent Nadeau <<a href="mailto:tanadeau@gmail.com" target="_blank">tanadeau@gmail.com</a>> wrote:<br>
> Why is the protocol for iterators called IteratorProtocol instead of<br>
> Iterator or Iterable? If that is/was discussed elsewhere, I apologize, but I<br>
> don't remember seeing that particular name before. Is that new to this<br>
> proposal?<br>
<br>
</span>I'm using the new names introduced by SE-0006<br>
<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0006-apply-api-guidelines-to-the-standard-library.md" rel="noreferrer" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/0006-apply-api-guidelines-to-the-standard-library.md</a><br>
<br>
The protocol is not called Iterator to disambiguate it with the<br>
Iterator associated type in Sequence from the protocol:<br>
<br>
protocol Sequence {<br>
associatedtype Iterator : IteratorProtocol<br>
}<br>
<br>
It is not called Iterable because that would have the wrong meaning<br>
(Sequence is iterable, the iterator iterates over its elements).<br>
<span><font color="#888888"><br>
Dmitri<br>
</font></span><div><div><br>
--<br>
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br>
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>>*/<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div></span><span class="HOEnZb"><font color="#888888">-- <br><div>Trent Nadeau</div>
</font></span></div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>