<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 27, 2016, at 2:41 PM, Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">on Mon Jun 27 2016, Matthew Johnson &lt;</span><a href="http://matthew-at-anandabits.com/" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">matthew-AT-anandabits.com</a><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">&gt; wrote:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class="">On Jun 27, 2016, at 11:46 AM, Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>&gt; wrote:<br class=""><br class=""><br class="">on Mon Jun 27 2016, Matthew Johnson &lt;<a href="http://matthew-at-anandabits.com/" class="">matthew-AT-anandabits.com</a><span class="Apple-converted-space">&nbsp;</span>&lt;<a href="http://matthew-at-anandabits.com/" class="">http://matthew-at-anandabits.com/</a>&gt;&gt; wrote:<br class=""><br class=""></blockquote><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Jun 26, 2016, at 10:56 PM, Jonathan Hull via swift-evolution<br class="">&lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Can’t a Sequence be potentially infinite, whereas a collection has a<br class="">defined count/endIndex? &nbsp;Other than that, I agree with your<br class=""></blockquote><br class=""><blockquote type="cite" class="">statement.<br class=""><br class="">Here is what I see as the appropriate structure:<br class=""><br class="">Iterator: Single destructive pass, potentially infinite, (should be for-in able)<br class="">Sequence: Guaranteed non-destructive multi-pass (vends Iterators),<br class="">potentially infinite, (should be subscript-able, gain most of<br class="">collection, but lose anything that relies on it ending)<br class="">Collection: Multi-pass, guaranteed finite, (no changes from current<br class="">form, except extra inits from Iterator/Sequence with end conditions)<br class=""><br class="">Right now we are allowed to have an infinite sequence, but calling<br class="">dropLast or non-lazy map will cause an infinite loop. &nbsp;These cases<br class="">could be made much safer by considering the potentially infinite and<br class="">finite cases separately…<br class=""></blockquote><br class="">I think this is pointing in the right general direction. &nbsp;It would<br class="">make working with `Sequence` much more straightforward and allow us to<br class="">depend on the multi-pass property that is true in practice of the most<br class="">common models of `Sequence`.<br class=""><br class="">But I agree that we should give much more careful consideration to<br class="">finite / infinite generally and for..in specifically.<br class=""><br class="">Now that I have been thinking about the finite / infinite distinction<br class="">more closely I have begun to notice a lot of code that is written<br class="">generically using `Sequence` where a for..in loop is really what is<br class="">required, however the “finite sequence” precondition is not explicitly<br class="">stated. &nbsp;Interestingly, this is the case with the standard library’s<br class="">eager `map` (but not the case with `dropLast` which explicitly notes<br class="">the precondition). &nbsp;I have been somewhat surprised to realize how<br class="">common this “bug” is (i.e. not stating a precondition). &nbsp;I think we<br class="">have gotten away with it thus far because the sequences most people<br class="">use most of the time in practice are finite. &nbsp;But that doesn’t mean we<br class="">should accept this as good enough - IMO it is way to easy to forget to<br class="">document this precondition (and obviously easier for users to overlook<br class="">than preconditions that are actually encoded in the type system,<br class="">violations of which are caught at compile time).<br class=""><br class="">The fact that this pattern is so pervasive is what I meant when I said<br class="">for..in “naturally” requires a finite sequence.<br class=""><br class="">IMO it’s better to encode preconditions in the type system when that<br class="">is practical, and especially when the precondition is shared by a vast<br class="">majority of code written using a particular construct (in this case a<br class="">for..in loop written using the most generic for..in-able protocol).<br class=""><br class="">I think the safest solution is to take the position that writing an<br class="">infinite loop is relatively uncommon and is a more “advanced”<br class="">technique, and thus should be done explicitly. &nbsp;Do people really write<br class="">infinite loops often enough that the convenience of using for..in when<br class="">writing infinite loops outweighs the safety benefit of preventing<br class="">accidental infinite loops? &nbsp;I haven’t seen a compelling argument for<br class="">this.<br class=""></blockquote><br class="">Good questions. &nbsp;I'd also add: “do infinite sequences come up often<br class="">enough that accidentally looping on them forever is a problem?”<br class=""></blockquote><br class="">That’s a good question as well. &nbsp;In practice the frequency of infinite<br class="">sequences likely depends on the domain.<br class=""><br class="">IMO this falls into the same category as “do single pass sequences<br class="">come up often enough that attempting to iterate over them twice is a<br class="">problem. &nbsp;To paraphrase your previous post:<br class=""><br class=""><blockquote type="cite" class="">Today, people see a beautiful, simple protocol (Sequence) to which many<br class="">things conform. They don't recognize that there's a semantic restriction<br class="">(you can’t assume it is finite!) on it, so they write libraries<br class="">of functions that may iterate a Sequence to termination. &nbsp;They test<br class="">their libraries with the most commonly-available Sequences, e.g. Arrays<br class="">and Ranges, which happen to be finite. &nbsp;Their tests pass! &nbsp;But their<br class="">constraints are wrong, their whole model of how to write generic code<br class="">over sequences is wrong, and some of their code is wrong.<br class=""><br class="">IMO this is a problematic programming model.<br class=""></blockquote><br class="">I definitely don’t mean to put words in your mouth here, but the<br class="">logical structure of the argument appears identical to me regardless<br class="">of which issue it is applied to. &nbsp;I am only trying to make that point.<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Oh, I fully agree. &nbsp;We're *in* this discussion because neither</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">single-pass nor infinite sequences come up very often. &nbsp;It raises the</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">question of whether the conceptual framework ought to accomodate them at</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">all, and if they should be included, how they should be represented.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div><div>We’ve considered good practical examples of both. &nbsp;They exist and people use them. &nbsp;I think we’re better off having a common vocabulary for them rather than requiring everyone who uses these concepts to invent their own. &nbsp;</div><div><br class=""></div><div>I agree that the question of how best to represent them is crucial.</div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">The quotation you cited above isn't arguing that single-pass sequences</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">are important enough to represent. &nbsp;It is trying to point out that the</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">refinement relationship between single- and multipass sequences has an</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">undesirable effect. &nbsp;I'm certain the same thing could occur for infinite</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">and finite sequences.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div><div>I’m not sure I agree. &nbsp;Neither are clearly modeled in the current library so I don’t think we know what the impact would be if they were. &nbsp;I’m willing to bet that people would choose the correct constraints if they were available (modeled accurately in the standard library, well documented, etc).</div><div><br class=""></div><div>What both of these *definitely* have in common is that they are purely semantic requirements that cannot be stated explicitly as requirements using syntax of the language. &nbsp;You have reminded us many times that protocols should not just be about syntax, but should also emphasize semantics. &nbsp;We shouldn’t shy away from introducing a protocol that doesn’t add any syntactic requirements if it is necessary to capture important semantic requirements.</div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">FWIW, Max pointed out to me on Friday that Scala's concept for</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">possibly-single-pass sequences is called “TraversibleOnce.” &nbsp;I think</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">that name goes a long way to solving the problem. &nbsp;I'm not sure how we'd</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">apply the same idea to finite/infinite sequences, though.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div><div>I’ve was thinking about how to best represent these while mowing the lawn this afternoon and had an interesting thought. &nbsp;If we add one additional protocol and pin down some of the semantics that have been discussed in this thread we could have a pretty straightforward model that captures the various semantics using a protocol in each of 4 quadrants:</div><div><br class=""></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Single Pass &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Multi Pass</div><div><br class=""></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>Potentially &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sequence</div><div>Infinite &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IteratorProtocol &nbsp;&lt;-------------------------| &nbsp; makeIterator()</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Collection</div><div>Finite &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FiniteIteratorProtocol &nbsp;&lt;------------------- &nbsp;makeIterator()</div><div><br class=""></div><div><br class=""></div><div>The semantic refinement on `Sequence` is that it must now produce a new iterator every time one is requested (and they must all produce identical sequences of values as long as the sequence was not mutated in between calls to `makeIterator`).</div><div><br class=""></div><div>The semantic refinement on `Collection` is that it must now be finite and its `Iterator` must conform to `FiniteIteratorProtocol` rather than `IteratorProtocol`.</div><div><br class=""></div><div>`FiniteIteratorProtocol` is the new protocol which introduces the semantic requirement that calls to `next` must “eventually” produce `nil`. &nbsp;“Eventually” might be a bit fuzzy but should carry the intent that your program won’t hang or be killed by the OS (i.e. diverge) if you iterate until `nil` is reached.</div><div><br class=""></div><div>You mentioned in another thread that for..in wouldn’t necessarily have to use a single protocol (or refinement relationship). &nbsp;Under this model I would propose that we make for..in operate on top of `FiniteIteratorProtocol`, but also have the ability to get the iterator from a `Collection`. &nbsp;(I’m using still advocating for restricting the for..in sugar to finite iteration but the same idea would work at the “potentially infinite” level using `IteratorProtocol and `Sequence`). &nbsp;The only change from current state (aside from the finite restriction) is to allow you to directly provide an iterator to the for..in loop rathe than requiring the compiler to get a new one via a call to `makeIterator`.</div><div><br class=""></div><div><div>If we do adopt something like this model, I think a couple of guidelines are clear regardless of whether for..in works exclusively with finite concepts or also admits potentially infinite ones. &nbsp;All code that requires terminating iteration should be written with `Collection` or `FiniteIteratorProtocol`, not `Sequence` or `IteratorProtocol`. &nbsp;Following from this, all finite sequences should conform to `Collection`, not just `Sequence` so it can continue to work with all code that requires finite sequences. &nbsp;One advantage of restricting for..in to the finite concepts is that it encourages people to follow this guideline so that their types will be usable with for..in, finite constraints, etc.</div><div><br class=""></div><div>In order to make it easy to follow the second guideline, the library should strive to make it as easy as possible to implement forward only `Collection` if you can vend an iterator and guarantee it is finite. &nbsp;Ideally it would be no more difficult than it is to implement `Sequence` today. &nbsp;Further customization of the implementation would be done as an optimization, not just to achieve basic capabilities.</div></div><div><br class=""></div><div>One other topic that came up is whether the single pass concepts should be required to have reference semantics. &nbsp;If we go in that direction it is clear that in the previous model `IteratorProtocol` would have a class requirement which would be inherited by `FiniteIteratorProtocol`.</div><div><br class=""></div><div>However, I am not convinced we should require reference semantics here. &nbsp;This requirement doesn’t exist today and I haven’t heard anyone say it has directly been a problem. &nbsp;The problem seems to be the due to the fact that `Sequence` is missing a semantic requirement everyone expects. &nbsp;I don’t know the rationale that was originally behind that decision and am curious. &nbsp;Is it because for..in works via `Sequence` and it was desirable to have for..in work with single pass sequences? &nbsp;The above suggested tweak to how for..in addresses that.</div><div><br class=""></div><div>Further, consider an algorithm which has multiple “choices” available and would like to be able to take advantage of backtracking. &nbsp;This can be achieved trivially with value semantic iterators by simply making a copy at any time during iteration and returning to the copy if a dead end is reached. &nbsp;You could sort of mimic this with a reference semantic iterator by conforming the iterator itself to `Sequence` or `Collection` and calling `makeIterator` to “clone” it. &nbsp;Obviously this would only be possible with intimate knowledge of the iterator and would usually not be possible retroactively, whereas it would be trivial to take advantage of iterators that have value semantics.</div><div><br class=""></div><div>-Matthew</div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">If we adopt that position then for..in would need to be built on top<br class="">of a guaranteed finite construct. &nbsp;This would allow programmers to<br class="">continue writing generic code agains the most generic for..in-able<br class="">construct while eliminating a precondition that is often (usually?)<br class="">unstated and likely unconsidered.<br class=""><br class="">If we do decide to move forward with infinite for..in loops I think we<br class="">need to establish strong guidance around how to properly write generic<br class="">code with these protocols. &nbsp;Should such code really be constrained to<br class="">`Collection` rather than `Sequence` (i.e. should a potentially<br class="">infinite `Sequence` have an eager map)? &nbsp;If this is the guidance,<br class="">should it be paired with guidance that all finite sequences should<br class="">conform to `Collection`? &nbsp;Or is it sufficient to just educate<br class="">developers about this issue and expect people to document the “finite<br class="">Sequence” precondition when the constraint is `Sequence` rather than<br class="">`Collection`?<br class=""><br class="">I hope we will give serious consideration to these questions while<br class="">this topic is open for discussion.<br class=""><br class="">-Matthew<br class=""><br class=""><blockquote type="cite" class=""><br class="">Thanks,<br class="">Jon<br class=""><br class=""><blockquote type="cite" class="">on Wed Jun 22 2016, David Waite &lt;<a href="http://david-at-alkaline-solutions.com/" class="">david-AT-alkaline-solutions.com</a><br class="">&lt;<a href="http://david-at-alkaline-solutions.com/" class="">http://david-at-alkaline-solutions.com/</a><span class="Apple-converted-space">&nbsp;</span>&lt;<a href="http://david-at-alkaline-solutions.com/" class="">http://david-at-alkaline-solutions.com/</a>&gt;&gt;&gt; wrote:<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Jun 22, 2016, at 2:57 PM, Dave Abrahams via swift-evolution<br class="">&lt;swift-evolution at<span class="Apple-converted-space">&nbsp;</span><a href="http://swift.org/" class="">swift.org</a><span class="Apple-converted-space">&nbsp;</span>&lt;<a href="http://swift.org/" class="">http://swift.org/</a>&gt;<br class="">&lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">&lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a>&gt;&gt;&gt;<br class="">wrote:<br class=""><br class="">&lt;Ahem&gt; “Iterators,” please.<br class=""></blockquote><br class="">That makes me happy - for some reason I thought it was still GeneratorProtocol<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">destructively, but such Generators would not conform to the needs of<br class="">Sequence. As such, the most significant impact would be the inability<br class="">to use such Generators in a for..in loop,<span class="Apple-converted-space">&nbsp;</span><br class=""></blockquote><br class="">Trying to evaluate this statement, it's clear we're missing lots of<br class="">detail here:<br class=""><br class="">* Would you remove Sequence?<br class="">* If so, what Protocol would embody “for...in-able?”<br class=""></blockquote>No, I would just remove the allowance in the documentation and API<br class="">design for a destructive/consuming iteration. Sequence would be the<br class="">interface to getting access to repeatable iteration, without the need<br class="">for meeting the other requirements for Collection.<br class=""></blockquote><br class="">That would be wrong unless there exist substantial examples of a<br class="">multipass Sequence that *can't* meet the other requirements of<br class="">Collection without loss of efficiency. &nbsp;And since I can write an adaptor<br class="">that turns any multipass sequence into a Collection, I think it's<br class="">trivial to prove that no such examples exist.<br class=""><br class="">--<span class="Apple-converted-space">&nbsp;</span><br class="">-Dave<br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class=""></blockquote><br class="">--<span class="Apple-converted-space">&nbsp;</span><br class="">-Dave<br class=""></blockquote><br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">--<span class="Apple-converted-space">&nbsp;</span></span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Dave</span></div></blockquote></div><br class=""></body></html>