<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">Le 2 avr. 2016 à 21:43, Andrew Bennett via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">On that note here is a convenient pattern I've used in the rare cases I haven't been able to convert to a "for in" syntax when refactoring:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(187,44,162)" class="">var</span> i = <span style="color:rgb(39,42,216)" class="">0</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(187,44,162)" class="">while</span> <span style="color:rgb(79,129,135)" class="">i</span> &lt; <span style="color:rgb(39,42,216)" class="">10</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">defer</span> { <span style="color:rgb(79,129,135)" class="">i</span> += <span style="color:rgb(39,42,216)" class="">1</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="color:rgb(61,29,129)" class="">print</span>(<span style="color:rgb(79,129,135)" class="">i</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div>To be honest I don't really mind this syntax, I often found during refactoring:</div><div class="">&nbsp;* the c-style "<span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px" class="">for</span>" was broken up over multiple lines anyway</div><div class="">&nbsp;* I wanted the last value of `<span style="font-family:Menlo;font-size:11px" class="">i</span>` outside the loop, so it was written "<span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px" class="">for&nbsp;</span><span style="font-family:Menlo;font-size:11px" class="">; check; incr"</span></div><div class="">&nbsp;* It still works with&nbsp;<span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px" class="">continue,</span>&nbsp;although it does increment "<span style="color:rgb(79,129,135);font-family:Menlo;font-size:11px" class="">i</span>" on&nbsp;<span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px" class="">break</span></div><div class=""><br class=""></div></div></div></blockquote><br class=""></div><div>Interesting pattern, it closely match with the "sugar" that C provides around ‘while’ in the form of 'for(;;)’</div><div><br class=""></div><div>The following C snippet</div><div><br class=""></div><div>#define INITIALIZER (i = 0)</div><div>#define TEST (i &lt; 10)</div><div>#define STEPPER (i += 1)</div><div><br class=""></div><div>for (INITIALIZER; TEST; STEPPER)</div><div>{</div><div>&nbsp; if (i % 2 == 0) { continue; }</div><div>&nbsp; if (i % 5 == 0) { break; }</div><div>&nbsp;&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">printf("%d\n",i);</span></div><div>}</div><div><br class=""></div><div>can be written (reusing the #define)</div><div><br class=""></div><div>INITIALIZER;</div><div>while TEST</div><div>{</div><div>&nbsp; if (i % 2 == 0) { STEPPER; continue; }</div><div>&nbsp; if (i % 5 == 0) { break; }</div><div>&nbsp;&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">printf("%d\n",i);</span></div><div>&nbsp; STEPPER;</div><div>}</div><div><br class=""></div><div>Beside the break issue, making use of ‘defer’ causes the nested continue to misbehave:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<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></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; loop_i:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> i &lt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">defer</span> { i += <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">defer</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"---"</span>,j) }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> j = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; loop_j:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> j &lt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">defer</span> { j += <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> j % <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">continue</span> loop_j }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> j % <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">continue</span> loop_i }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(i,j)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div></div><div>In this example, the ‘continue loop_i’ does increase ‘j’, but I would not expect that; since C doesn't &nbsp;allow to directly continue an outer loop I cannot compare my opinion against the real world.</div><div><br class=""></div><div>Should we pursue having a ‘defer(@oncontinue)’ or a new ‘continuer’ (don’t really like that name)?</div><div><br class=""></div><div>With this lone new syntax, all the imaginable 'for(;;)’ can be blindly ported to Swift, with all the advantages and disadvantages of the C version. Reusing my #define as placeholders, any C-world ‘for(;;)’ can be written in Swift as:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>INITIALIZER</div><div><span class="Apple-tab-span" style="white-space: pre;">        </span>while TEST</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>defer(@oncontinue) { STEPPER }</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// Work load</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}&nbsp;</div><div><br class=""></div><div>In a nested loop environment, this could be used like:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<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></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; loop_i:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> i &lt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">defer</span>(@onncontinue) { i += <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">var</span> j = <span style="color: rgb(39, 42, 216);" class="">0</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; loop_j:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> j &lt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">defer</span>(@oncontinue) { j += <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> j % <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">continue</span> loop_j <span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">/* j += 1 */</span>}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> j % <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">continue</span> loop_i <span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">/* i += 1, but NOT j += 1*/</span>}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(i,j)</div><div style="margin: 0px; font-size: 11px; 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; &nbsp; &nbsp; &nbsp; &nbsp; </span>// continue loop_j // implicit, so j += 1</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; 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; &nbsp; &nbsp; </span>// continue loop_i // implicit, so i += 1</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">Dany</div></div><div><br class=""></div></body></html>