<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=""><div><blockquote type="cite" class=""><div class="">On Apr 12, 2016, at 4:15 AM, Dmitri Gribenko via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">On Mon, Apr 11, 2016 at 9:56 PM, Brent Royal-Gordon via<br class="">swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><blockquote type="cite" class="">(On the other hand, it might be that I'm conceiving of the purpose of `limitedBy` differently from you—I think of it as a safety measure, but you may be thinking of it specifically as an automatic truncation mechanism.)<br class=""></blockquote><br class="">Hi Brent,<br class=""><br class="">Could you explain what kind of safety do you have in mind? Swift will<br class="">guarantee memory safety even if you attempt to advance an index past<br class="">endIndex using the non-limiting overload.<br class=""></div></div></blockquote><div><br class=""></div><div>One challenge that I've run into is that the `limitedBy` methods throw away one bit of information—namely, did I move as far as I requested or not? Example:</div><div><br class=""></div><div><font face="Menlo" class="">let j = c.index(10, stepsFrom: i, limitedBy: c.endIndex)</font></div><div><br class=""></div><div>There's no way to interpolate the answer to that question efficiently in a non-random-access collection. If `j` is equal to `c.endIndex`, that could be because `c.endIndex` is ten steps after `i` *or* because the limit kicked in, and without checking `c.distance(from: i, to: j)` there's no way to know for sure.</div><div><br class=""></div><div>If the `limitedBy` methods returned an optional index, we'd get all the information that the index-moving algorithm finds (let's hear it for the Law of Useful Return!). With that API, we could decide whether to use the returned index or not:</div><div><br class=""></div><div><div><font face="Menlo" class="">// Use the resulting index no matter what:</font></div><div><font face="Menlo" class="">let i = c.index(10, stepsFrom: c.startIndex, limitedBy: c.endIndex) ?? c.endIndex</font></div><div class=""><font face="Menlo" class="">let prefix = c.prefix(upTo: i)</font></div></div><div><br class=""></div><div><font face="Menlo" class="">// Only use the result if it wasn't limited:</font></div><div><font face="Menlo" class="">if let j = c.index(10, stepsFrom: i, limitedBy: c.endIndex) {</font></div><div><font face="Menlo" class=""> let sub = c[i..<j] // sub.count == 10</font></div><div><font face="Menlo" class="">} else {</font></div><div><font face="Menlo" class=""> // not enough elements...</font></div><div><font face="Menlo" class="">}</font></div><div><font face="Menlo" class=""><br class=""></font></div><div><div><span style="font-family: Menlo;" class="">// "Safe" successor:</span></div></div><div><font face="Menlo" class="">if let j = c.index(1, stepsFrom: i, limitedBy: c.endIndex) {</font></div><div><font face="Menlo" class=""> // ...</font></div><div><font face="Menlo" class="">}</font></div><div><br class=""></div><div>Nate</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div class="">Dmitri<br class=""><br class="">-- <br class="">main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br class="">(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" class="">gribozavr@gmail.com</a>>*/<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=""></div></div></blockquote></div><br class=""></body></html>