<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I've also thought of this idea before, but how often in code do you actually repeat something a given number of times? There are only a few use cases I can think of:</div><div class=""><br class=""></div><div class="">- retries before giving up on something</div><div class="">- emitting padding when doing formatted output of some kind</div><div class="">- <i class="">reading</i>&nbsp;formatted data when you have a given number of rows/records to read</div><div class=""><br class=""></div><div class="">If it's that rare, I'm not sure it's worth dedicating syntax to.</div><div class="">Jordan</div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 8, 2015, at 13:54, Jason Pollack via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I'd like to propose a small enhancement to the repeat loop.<div class=""><br class=""></div><div class="">Currently if we want to perform 'count' iterations in a loop, we need to do something like:</div><div class=""><br class=""></div><div class="">for _ in 0 ..&lt; count {</div><div class="">&nbsp; &nbsp;//Do something here</div><div class="">}</div><div class=""><br class=""></div><div class="">It looks and feels a little awkward. We need to create an unnamed variable, and it's easy to forget (especially for language newcomers) that the loop starts with 0 and doesn't include count.</div><div class=""><br class=""></div><div class="">We can also do:</div><div class=""><br class=""></div><div class="">var i = 0</div><div class="">repeat {</div><div class="">&nbsp; &nbsp; //Some code</div><div class="">&nbsp; &nbsp; i += 1</div><div class="">} while i &lt; 10</div><div class=""><br class=""></div><div class="">This is worse, in that it introduces a variable in the outer scope, and puts the repeat count at the end. Plus, if the expression inside the repeat is complex, the value of i may never be incremented, or may be incremented more than once.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">I propose the following:</div><div class=""><br class=""></div><div class="">repeat count {</div><div class="">&nbsp; &nbsp; //Do something here</div><div class="">}</div><div class=""><br class=""></div><div class="">It's cleaner, and IMO clearer what this code is intended to do.</div><div class=""><br class=""></div><div class="">Naturally 'count' should be non-negative. A while clause is not needed, although I could imagine it being supplied, to create a construct such as:</div><div class=""><br class=""></div><div class="">var ok = true</div><div class="">repeat numberOfTimes {</div><div class="">&nbsp; &nbsp; //Do something, possibly set ok to false</div><div class="">} while ok</div><div class=""><br class=""></div><div class="">This would repeat the loop a maximum of &nbsp;'numberOfTimes', but could be ended early if some signal 'ok' is set to false (or, of course, by a break statement).</div><div class=""><br class=""></div><div class="">Thoughts?</div><div class=""><br class=""></div><div class="">Thanks!</div><div class="">-Jason-</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=f6VunEbxOyO0-2FacKqVTeu6pGEEellWH1YmuP-2Bt5lLhNtmIXF-2BaM45P9puL1WYbUmrdej-2F87olnZt0sRLdwJH7jTeozr4Sr0qhjZK-2BXxD4gXfNAInwgHkMSsJhlAzB5UFrHmXSNox-2B3Sq4JVS0ZYe9IPUJGmWaO5KpLgCMl2E0j1W9fKfq9b5p7g7myqY8nrIo6WtXYXOZk2AsFgB0JxdU5A114lZcySuMS6Oa5-2B5A3c-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" 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></blockquote></div><br class=""></body></html>