<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: [() -> <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>..<<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=""> <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=""> handler() <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 < <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=""> <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=""> handler() </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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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 <i class="">for</i> loop should be removed from Swift. The scope rules for this <i class="">for</i> 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 <i class="">for</i> 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> handlers: [() -> ()] = []</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> i <span class="" style="color: rgb(187, 44, 162);">in</span> <span class="" style="color: rgb(39, 42, 216);">0</span>..<<span class="" style="color: rgb(39, 42, 216);">5</span> {</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(79, 129, 135);">handlers</span>.<span class="" style="color: rgb(61, 29, 129);">append</span> { <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> handler <span class="" style="color: rgb(187, 44, 162);">in</span> <span class="" style="color: rgb(79, 129, 135);">handlers</span> {</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"> handler() <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=""> = []</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> <span class="" style="color: rgb(187, 44, 162);">var</span> i = <span class="" style="color: rgb(39, 42, 216);">0</span>; i < <span class="" style="color: rgb(39, 42, 216);">5</span>; i += <span class="" style="color: rgb(39, 42, 216);">1</span> {</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(79, 129, 135);">handlers</span>.<span class="" style="color: rgb(61, 29, 129);">append</span> { <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> handler <span class="" style="color: rgb(187, 44, 162);">in</span> <span class="" style="color: rgb(79, 129, 135);">handlers</span> {</div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"> handler() <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 <i class="">for-in</i> loop does the right thing naturally. The C-style <i class="">for</i> loop does the wrong thing naturally. Removing the C-style <i class="">for</i> 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>