<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>+1 for <i>repeat X { } (</i>as a keyword/statement, not a function).<br><br>It's definitely syntax sugar, but I agree it would give beginners an easy "in" to for loops. It would also make some of the code we write a bit smaller and more readable, which is always a good thing. I haven't seen any serious detriments put forward either.</div><div><br></div><div>Liam</div><div><br><div>Sent from my iPhone</div></div><div><br>On Dec 9, 2015, at 8:30 AM, Daniel Steinberg via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><span>Thanks Kenny</span><br><span></span><br><span>It’s so difficult for us to remember when these constructs weren’t familiar to us - to remember how much complexity there is in this code:</span><br><span></span><br><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>for x in 1...5 {</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;… some code…</span><br></blockquote><blockquote type="cite"><span>}</span><br></blockquote><span></span><br><span>for x in 1…5 - what does 1 … 5 mean? What does it mean for x to be in 1 … 5? Now you tell me x isn’t in 1 … 5 and I can replace x with _ &nbsp;what does it mean for _ to be in 1 … 5 and why do I need _ at all? _ isn’t in the loop either. What’s a loop?</span><br><span></span><br><span>That’s not to say I haven’t taught this - I’m saying it’s a lot harder for someone new to programming to grasp than many people think. </span><br><span></span><br><span>I understand that it’s not a goal of the language to introduce syntax we only use in passing until they are ready to understand the for syntax so I won’t continue to argue the point. I was just voicing my support for the proposal and adding a use case where it might apply.</span><br><span></span><br><span>Best,</span><br><span></span><br><span>Daniel</span><br><span></span><br><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>If you’re teaching to newcomers:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>- there’s no need to explain that counting starts at 0 because it doesn’t</span><br></blockquote><blockquote type="cite"><span>- if “x” is not used within the loop, then they will get a warning. That would be the perfect time to explain that they can just replace index with “_” when you don’t need to use the value within the loop.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>BTW, I hate the “in x ..&lt; count” syntax. If you mean count from 0 to 4, then just write 0…4</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>-Kenny</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2 Prerelease).” iBooks. </span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><blockquote type="cite"><span>On Dec 8, 2015, at 1:54 PM, Jason Pollack via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>I'd like to propose a small enhancement to the repeat loop.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Currently if we want to perform 'count' iterations in a loop, we need to do something like:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>for _ in 0 ..&lt; count {</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span> &nbsp;//Do something here</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>}</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>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.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>We can also do:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>var i = 0</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>repeat {</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span> &nbsp;&nbsp;//Some code</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span> &nbsp;&nbsp;i += 1</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>} while i &lt; 10</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>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.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>I propose the following:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>repeat count {</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span> &nbsp;&nbsp;//Do something here</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>}</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>It's cleaner, and IMO clearer what this code is intended to do.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>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:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>var ok = true</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>repeat numberOfTimes {</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span> &nbsp;&nbsp;//Do something, possibly set ok to false</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>} while ok</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>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).</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Thoughts?</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Thanks!</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>-Jason-</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>_______________________________________________</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>swift-evolution mailing list</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></blockquote></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>_______________________________________________</span><br></blockquote><blockquote type="cite"><span>swift-evolution mailing list</span><br></blockquote><blockquote type="cite"><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br></blockquote><blockquote type="cite"><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></blockquote><span></span><br><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>