<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">That is very odd indeed.</div><div class=""><br class=""></div><div class="">If you add explicit capture, the issue goes away:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> handlers: [() -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Void</span>] = []</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> i <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>..&lt;<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span> {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">handlers</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">append</span> {[i] <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(i, terminator:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">","</span>) }</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> handler <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">handlers</span> {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; handler()&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// "0 1 2 3 4"</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">handlers<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = []</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> i = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>; i &lt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span>; i += <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">handlers</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">append</span> {[i] <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(i, terminator:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">","</span>) }</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class="">print<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">""</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> handler <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">handlers</span> {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; handler()&nbsp; </span>// was "5 5 5 5 5", now 1, 2, 3, 4, 5</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">It really seems like a bug to me rather than a feature of the for-loop</div><div class=""><br class=""></div><div class="">-- Erica</div><div class=""><br class=""></div><br class=""><div class=""><div class=""></div></div><blockquote type="cite" class=""><div class=""><div class="">On Dec 6, 2015, at 12:03 PM, Kelly Gerber 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 class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">I think that the C-style&nbsp;<i class="">for</i>&nbsp;loop should be removed from Swift. The scope rules for this&nbsp;<i class="">for</i>&nbsp;loop are wrong. Every loop sees the same scope. This is a source of bugs if you export the loop variable name outside the scope of the&nbsp;<i class="">for</i>&nbsp;statement, for example in a closure. The following code illustrates the problem:<div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">var</span>&nbsp;handlers: [() -&gt; ()] = []</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 16px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">for</span>&nbsp;i&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;<span class="" style="color: rgb(39, 42, 216);">0</span>..&lt;<span class="" style="color: rgb(39, 42, 216);">5</span>&nbsp;{</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;&nbsp;<span class="" style="color: rgb(79, 129, 135);">handlers</span>.<span class="" style="color: rgb(61, 29, 129);">append</span>&nbsp;{&nbsp;<span class="" style="color: rgb(61, 29, 129);">print</span>(i) }</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 16px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">for</span>&nbsp;handler&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;<span class="" style="color: rgb(79, 129, 135);">handlers</span>&nbsp;{</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; handler()&nbsp;&nbsp;<span class="" style="color: rgb(0, 132, 0);">// "0 1 2 3 4"</span></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 16px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);">handlers<span class="">&nbsp;= []</span></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 16px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">for</span>&nbsp;<span class="" style="color: rgb(187, 44, 162);">var</span>&nbsp;i =&nbsp;<span class="" style="color: rgb(39, 42, 216);">0</span>; i &lt;&nbsp;<span class="" style="color: rgb(39, 42, 216);">5</span>; i +=&nbsp;<span class="" style="color: rgb(39, 42, 216);">1</span>&nbsp;{</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;&nbsp;<span class="" style="color: rgb(79, 129, 135);">handlers</span>.<span class="" style="color: rgb(61, 29, 129);">append</span>&nbsp;{&nbsp;<span class="" style="color: rgb(61, 29, 129);">print</span>(i) }</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 16px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">for</span>&nbsp;handler&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;<span class="" style="color: rgb(79, 129, 135);">handlers</span>&nbsp;{</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; handler()&nbsp;&nbsp;<span class="" style="color: rgb(0, 132, 0);">// "5 5 5 5 5"</span></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;">}</div></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">The Swift&nbsp;<i class="">for-in</i>&nbsp;loop does the right thing naturally. The C-style&nbsp;<i class="">for</i>&nbsp;loop does the wrong thing naturally. Removing the C-style&nbsp;<i class="">for</i>&nbsp;loop from Swift will eliminate one more class of possible errors from the language.</div><img alt="" width="1" height="1" border="0" class="" style="border-width: 0px !important; margin: 0px !important; padding: 0px !important;" apple-inline="yes" id="CE7B51A9-F80D-4767-B2B0-C74FD843F612" apple-width="yes" apple-height="yes" src="cid:02E8910D-9234-4025-8EC2-A5809BF85FC1@hsd1.co.comcast.net"></div>_______________________________________________<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 class=""><br class=""></div></body></html>