<div dir="ltr"><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">Hello ​Swift Evolution community,​</div><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">Thank you so much for all the important feedback, this thread became more lively than I initially hoped for!</div><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">First let me address Xiaodi Wu&#39;s questions from earlier: </div><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div></div><blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:arial,sans-serif;margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">What is your motivation for this change?</span></blockquote><div class="gmail_default"><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px"><br></span></div><div class="gmail_default">After thinking about it maybe I overreacted with wanting to phase out `while true`.</div><div class="gmail_default">I know `while true` is a programming idiom found in most languages but I personally think expressions testing a single `Bool` literal</div><div class="gmail_default">like `while true { ... }` are just as tasteless as directly using `true` as in `if true { ... }` and might possibly signify that swift needs a better construct to express an infinite loop.</div><div class="gmail_default"><br></div><div class="gmail_default">So after reading this thread&#39;s feedback I&#39;m changing my tone and think we should leave `while true` alone and let it continue to be</div><div class="gmail_default">a valid way of expressing an infinite loop but maybe at the same time we should think of adding a more Swifty alternative.</div><div class="gmail_default"><br></div><div class="gmail_default">As a source of inspiration I really like rust&#39;s solution of `loop { ... }`: <a href="https://doc.rust-lang.org/book/loops.html">https://doc.rust-lang.org/book/loops.html</a></div><div class="gmail_default">but Swift already has a `repeat` statement so I initially thought of modifying the behaviour of `repeat` to allow omitting the trailing `while true` as to not bloat Swift with a new keyword.</div><div class="gmail_default"><br></div><blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:arial,sans-serif;margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">What problems are solved by forbidding `while true`?</span></blockquote><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19);display:inline">​As I said above, after thinking about it again I think we should keep `while true` but I want to talk about a possible issue with the</div></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19);display:inline">`while true` idiom:</div></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"><br></span></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"><div class="gmail_default" style="display:inline">​    ​</div>let</span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"> foo: </span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">Int<div class="gmail_default" style="display:inline">​ </div></span></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"><div class="gmail_default" style="display:inline">    ​</div></span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">while</span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"> </span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">true</span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"> { </span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">foo</span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"> = </span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">5</span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">; </span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">break</span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"> }</span></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"><div class="gmail_default" style="display:inline">​    ​</div>print</span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">(</span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">foo</span><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif">)</span></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><span class="" style="color:rgb(39,78,19);font-family:&#39;comic sans ms&#39;,sans-serif"><br></span></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">​The above code will error out with the message &quot;Constant &#39;foo&#39; used before being initialized&quot;​</div><div class="gmail_default" style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">`while true` will always enter the body of the loop at least once but since `while` is a conditional statement this error occurs</div></div><div class="gmail_default">whereas this code works just fine:</div><div class="gmail_default"><br></div><div class="gmail_default"><div class="gmail_default">    let foo: Int</div><div class="gmail_default">    repeat { foo = 5; break } while true</div><div class="gmail_default">    print(foo)</div><div class="gmail_default"><br></div><div class="gmail_default">since `repeat` will unconditionally enter the repeat block at least once. </div><div class="gmail_default">So I think my initial idea can be broken up into two distinct parts.</div><div class="gmail_default"><br></div><div class="gmail_default">Part I: add a `while true` alternative</div><div class="gmail_default"><div class="gmail_default">----------------------------------------------------------------------------------------</div></div><div class="gmail_default"><br></div><div class="gmail_default">So for the `while true` idiom replacement I want to propose adding something like a `loop { }` construct or possibly modifying the `repeat` keyword to take a statement modifier such as `sustained repeat { ... }`</div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_default">Part II: change `repeat` statement ordering OR  `while` statement modifier</div><div class="gmail_default">----------------------------------------------------------------------------------------</div><div class="gmail_default"><br></div><div class="gmail_default">Idea #1:</div><div class="gmail_default"><br></div><div class="gmail_default">Add a statement modifier to `while` that would ignore the `while` loop&#39;s condition only</div><div class="gmail_default">on the first run-through.</div><div class="gmail_default">Not too sure on the naming of the statement modifier but maybe something something like `after while foo &gt; bar { ... }` </div><div class="gmail_default"><br></div><div class="gmail_default">`after` would signify that the while loops condition would be checked only after the first iteration. </div><div class="gmail_default"><br></div><div class="gmail_default">Idea #2:</div><div class="gmail_default"><br></div><div class="gmail_default">change `repeat { ... } while ...` to `repeat while ... { ... }` so that the loop condition can be seen upfront in long blocks, </div><div class="gmail_default">adding a `repeat` in front of the while loop would ensure that the loop runs once before evaluating the loop condition.</div><div class="gmail_default">This would essentially remove the repeat keyword and make it a statement modifier like the above written `after`. </div><div class="gmail_default"><br></div><div class="gmail_default">Sorry for dragging this out a little after all the great feedback but does either idea have any appeal to the Swift community?</div><div class="gmail_default"><br></div><div class="gmail_default">Thanks!</div><div class="gmail_default"><br></div><div class="gmail_default">- Nick </div><div class="gmail_default"><br></div></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"> </div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"> </div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif"> </div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_extra" style="color:rgb(34,34,34);font-family:arial,sans-serif"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><br><div>All the best,</div><div><br></div><div>Nicholas</div><div><br></div><div>Linked in:</div><div><font size="2"><a href="http://lnkd.in/328U22" target="_blank">http://lnkd.in/328U22</a></font><br></div><div><br></div></div></div></div><br><div class="gmail_quote">On Tue, May 10, 2016 at 4:39 PM, Xiaodi Wu <span dir="ltr">&lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="">On Tue, May 10, 2016 at 2:27 AM, Nicholas Maccharoli via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">​Swift Evolution ​Community,</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">Currently writing an infinite loop in swift looks either something like this:</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">    while true {</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">        if ... { break }</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">        //...</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">    }</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">Or this:</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">    repeat {</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">        if ... { break }</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">        //...</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">    } while true</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">But I think it might be best to change the syntax / behaviour of `repeat` to loop </div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">indefinitely if no trailing while clause is present:</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">    repeat {</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">        if ... { break }</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">        //...</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">    }</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">while still allowing a trailing `while` clause as in:</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">    repeat { </div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">        foo += bar</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">    } while foo.count &lt; limit </div></div></blockquote><div> </div></span><div>What is your motivation for this change?</div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">I also want to propose that it should be a compile time error to use single `Bool` constants as while loop conditions, so no more `while true { ... }` it would become `repeat { ... }`</div></div></blockquote><div><br></div></span><div>What problems are solved by forbidding `while true`?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><div dir="ltr"><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">I was thinking of drafting a short proposal if there was enough positive feedback. <br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">How does it sound?</div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)"><br></div><div style="font-family:&#39;comic sans ms&#39;,sans-serif;color:rgb(39,78,19)">- Nick </div></div><br></span>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">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></div></blockquote></div></div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><br><div>All the best,</div><div><br></div><div>Nicholas</div><div><br></div><div>Linked in:</div><div><font size="2"><a href="http://lnkd.in/328U22" target="_blank">http://lnkd.in/328U22</a></font><br></div><div><br></div></div></div></div>
<br><div class="gmail_quote">On Wed, May 11, 2016 at 10:11 AM, Tyler Cloutier via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
&gt; On May 10, 2016, at 5:56 PM, Chris Lattner &lt;<a href="mailto:clattner@apple.com">clattner@apple.com</a>&gt; wrote:<br>
&gt;<br>
&gt;<br>
&gt;&gt; On May 10, 2016, at 4:13 PM, Cole Campbell via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; I agree that repeat { } is ambiguous because you have to look to the end for a while clause to determine if it&#39;s infinite or not.<br>
&gt;<br>
&gt; Right, this is the downside that I see with “repeat {}”.<br>
<br>
<br>
</span>Not to beat a dead horse, but isn’t this also true of<br>
<br>
repeat {<br>
<br>
} while true<br>
<br>
and<br>
<br>
while true {<br>
   ...<br>
   ...<br>
   if condition {<br>
        break<br>
<span class="">   }<br>
}<br>
<br>
&gt;<br>
&gt;&gt; while true { } is preferable in that regard, but a compromise that I saw mentioned is:<br>
&gt;&gt;<br>
&gt;&gt; repeat forever { }<br>
&gt;<br>
&gt; This would require taking “forever” as a keyword if we supported “repeat N {&quot;, something we wouldn’t want to do.<br>
&gt;<br>
&gt; Another option is to make it a statement modifier, which wouldn’t require taking it as a keyword (but also doesn’t read as well):<br>
&gt;<br>
&gt; forever repeat { }<br>
&gt;<br>
&gt;<br>
&gt; Personally, I don’t see this as a big enough improvement over “while true” to be worth introducing complexity for.<br>
&gt;<br>
<br>
</span>If you are referring to “forever&quot;, I also don’t think that adding a new keyword is an improvement over “while true”.<br>
<div class="HOEnZb"><div class="h5"><br>
&gt; -Chris<br>
<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>
</div></div></blockquote></div><br></div>