<div dir="ltr"><div>Well, it is possible to just add an extra scope around the variable and the loop:<br><br>do {<br> var i = 0<br> while i < 10 {<br> print(i)<br> i += 1<br> }<br>}<br><br></div>—Johan</div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Dec 6, 2015 at 10:13 PM, Tyler Fleming Cloutier via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> 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"><div><span class=""><br><blockquote type="cite"><div style="font-family:Helvetica;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;font-size:16px"><b>Lowered Readability and Maintainability</b></div><div style="font-family:Helvetica;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;font-size:16px"><br></div><div style="font-family:Helvetica;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;font-size:16px">I have aesthetic reasons for disliking the for-loop. The C-style loop is harder to read especially for those not coming from C-style languages, easier to mess up at edge conditions, and is commonly used for side-effects which, in a language focused on safety, is not a feature to be encouraged. </div><div style="font-family:Helvetica;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;font-size:16px"><br></div><div style="font-family:Helvetica;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;font-size:16px">For example, one side effect that was mentioned on-list yesterday was the incrementor, which is guaranteed in C-style to execute late:</div><div style="font-family:Helvetica;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"><div style="font-size:16px">Roland King writes:</div><blockquote style="font-size:14px;margin:0px 0px 0px 40px;border:none;padding:0px"><div style="font-size:16px"><br></div><div style="font-size:16px"><div style="font-family:Palatino-Roman">for var floatingThing = start ; floatingThing <= end ; floatingThing += delta</div><div style="font-family:Palatino-Roman">{</div><div style="font-family:Palatino-Roman"><span style="white-space:pre-wrap">        </span>// more than a few lines of code with early escape continues</div><div style="font-family:Palatino-Roman">}</div><div style="font-family:Palatino-Roman"><br></div><div style="font-family:Palatino-Roman">shows intent of the loop very clearly, start, condition and increment all together at the top, and however you loop you always execute the increment part of the statement. Convert that to a while(), if you have a continue in the body, you have a good chance of not incrementing at all, or duplicating the increment code before every continue. So you can’t always nicely turn for into while. I second the point below about the loop variable being local to the for as well, I also like that. </div><div style="font-family:Palatino-Roman"><br></div></div></blockquote><span style="font-size:16px">Late incrementor management is a feature that can be mimicked with defer, as pointed out by several other list members.</span><font face="Palatino-Roman" size="3"><br></font><blockquote style="font-size:14px;margin:0px 0px 0px 40px;border:none;padding:0px"><div style="font-size:16px"><div><br></div></div></blockquote></div></blockquote><div><br></div></span><div>Defer wouldn’t accomplish the exact same behavior because it would run if an exception was thrown, which is not the same as the last clause of a for loop, but perhaps is close enough.</div><div><br></div><div>The only other concern I would have is not being able to scope my variables to the loop. I didn’t see it addressed, but perhaps its not very important in the end anyway.</div><div><br></div><div>Something like the following might be nice to scope the variable exclusively to the loop.</div><div><br></div><div>for var x = 0 while (someCondition()) {</div><div><span style="white-space:pre-wrap">        </span>// code</div><div>}</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=KlmFWKNIEcyPEGx2Wqruu-2FaM6I0anrxIOlKS1pgqec5RYj-2Bp64B-2FvfIITaGIMUuO-2BPklpMZsuO49zxAyeqr0hq6X4CN2pJdmR9O94FXsd63ciq-2B-2Frs1GxzN-2BFXoWJ6X3mdKa1ROM0vcGn1y-2Fe9u9VdXWV71O64dDe7abBExBBF2w8I1dqXbwxg3vZP5kSh8mYr25TW9K07Jd9p69BdXSkqTWU6YUrubLDNO7G3XYN-2Fk-3D" alt="" style="min-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" height="1" border="0" width="1">
</div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>