<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>+1</div><div><br></div><div>-Thorsten </div><div><br>Am 20.01.2016 um 02:32 schrieb Howard Lovatt via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div>+1 from me. I have been caught by this. In the degenerate case I didn't want the loop to be processed, but instead I got a runtime error. I solved the problem by changing to a C style for loop, but with the removal of C style for loops this will be more of a problem. <div><br></div><div>This could be solved with a library function as already suggested, however having `...`, `..<`, `...?`, and `..<?` seems excessive. Therefore my suggestion is to make `...` & `..<` behave as proposed (i.e. return empty ranges when limits mean nothing enclosed by range).<br><br>On Wednesday, 20 January 2016, Félix Cloutier <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I don't have a strong opinion on this, but maybe there could be a ..<?/...? operator for ranges that may or may not be well-formed? This solution can be implemented "at home" too.<div><br></div><div>I'd love to see where and how developers use ranges. It may be more helpful than it looks like. For instance, if you use a range to get an array slice, would your rather have an empty slice if your range underflows instead of the current error behavior?<br><div>
<br><span style="color:rgb(0,0,0);font-family:'Lucida Grande';font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;display:inline!important;float:none">Félix</span>
</div>
<br><div><blockquote type="cite"><div>Le 19 janv. 2016 à 15:46:00, Uwe Falck via swift-evolution <<a href="javascript:_e(%7B%7D,'cvml','swift-evolution@swift.org');" target="_blank">swift-evolution@swift.org</a>> a écrit :</div><br><div><div>I’m looking for feedback on this request if its worth to start an evolution proposal.<br><br><br>Let range operators always return empty ranges if the upper bound is smaller than the lower bound.<br><br> ####<br><br> Introduction<br><br> ####<br><br>Consider two loops. The first loop iterator will return an empty range and will not be executed. The second loop throws an error. I would like to see the range iterator always returning an empty range if end index < start index. <br><br>for i in 3..<3 <br>{ print(i) }<br><br>for i in 3…2<br>{ print(i) }<br><br><br> ####<br><br> Motivation<br><br> ####<br><br>The two expressions above are mathematically equivalent and I would like them to return the same result for consistency.<br><br>Furthermore, and more important: if C-style for loops are gone with Swift 3.0, programmers may translate <br><br>func fibonacci(n: Int) -> Int { // works for n>=0<br><span style="white-space:pre-wrap">        </span>var memo = [0,1]<br><span style="white-space:pre-wrap">        </span>for var i = 2; i <= n; i++ {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>memo.append(memo[i-1] + memo[i-2])<br><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span>return memo[n]<br>}<br><br>probably into<br><br>func fibonacci(n: Int) -> Int {<span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>// works only for n>=2!<br><span style="white-space:pre-wrap">        </span>var memo = [0,1]<br><span style="white-space:pre-wrap">        </span>for i in 2...n {<br><span style="white-space:pre-wrap">        </span><span style="white-space:pre-wrap">        </span>memo.append(memo[i-1] + memo[i-2])<br><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span>return memo[n]<br>}<br><br>This example is from Stackoverflow[1] with two suggested solutions to prevent the runtime error for 0 and 1<br><br>let startIndex = 2<br>let endIndex = n<br>for i in startIndex.stride(through: endIndex, by: 1) {<br><span style="white-space:pre-wrap">        </span>memo.append(memo[i-1] + memo[i-2])<br>}<br><br>…and another one uses the empty range generate by ..<<br><br>for i in 2 ..< max(2, n+1) {<br><span style="white-space:pre-wrap">        </span>memo.append(memo[i-1] + memo[i-2])<br>}<br><br>Clearly the not-working-solution looks most logical. All other control flow elements, like while, will just not execute if their condition is not met on loop entry.<br><br><br> #####<br><br> Proposed solution<br><br> #####<br><br>Let both range iterators return emtpy ranges, if end index < start index, and not only for a..<b with a==b.<br><br> #####<br><br> Impact on existing code<br><br> #####<br><br> None.<br><br> ####<br><br> Alternatives considered<br><br> ####<br><br> If range operators will allow downward variants this idea becomes pointless.<br><br> ####<br><br> Open questions<br><br> ####<br><br> None.<br><br><br> [1] <a href="http://stackoverflow.com/questions/34323227/a-concise-way-to-not-execute-a-loop-now-that-c-style-for-loops-are-going-to-be-r?lq=1" target="_blank">http://stackoverflow.com/questions/34323227/a-concise-way-to-not-execute-a-loop-now-that-c-style-for-loops-are-going-to-be-r?lq=1</a><br><br><br> Thanks,<br><br><br> --<br><br> Uwe<br><br>_______________________________________________<br>swift-evolution mailing list<br><a href="javascript:_e(%7B%7D,'cvml','swift-evolution@swift.org');" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></div></blockquote></div><br></div></div></blockquote></div><br><br>-- <br> -- Howard.<br><br>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>