<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=""><div class="">Just to add my thoughts, as I like the idea of adding the variables to the start somehow, but was wondering if might make sense to have a keyword such as "using", but allow it on all block statements, like-so:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Original use-case of repeat … while</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>repeat using (var i = 0) {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>// Do something</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}&nbsp;while (i &lt; 20)</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// for … in demonstrating combination of using and where</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>for eachItem in theItems using (var i = 0) where (i &lt; 20) {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>// Do something either until theItems run out or i reaches 20</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Standard while loop</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>while let eachItem = it.next() using (var i = 0) where (i &lt; 20) {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>// As above, but with an iterator and a while loop and conditional binding to also stop on nil</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Closure with its own captured variable</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let myClosure:(Int) -&gt; Int = using (var i = 0) { i += 1; return i * $0 }</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// If statements as well</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if somethingIsTrue() using (var i = 0) where (i &lt; 20) {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>// Do something</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Or even a do block; while it does nothing functionally new, I quite like it aesthetically</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>do using (var i = 0) {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>// Do something</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><br class=""></div><div class="">Unifying principle here is that anything created in the using clause belongs to the loop, conditional branch etc. only, but exists outside the block itself (thus persisting in the case of loops and closures). I quite like the possible interaction with where clauses here as a means to avoid simple inner conditionals as well.</div><div class=""><br class=""></div><div class="">Basically the two clauses can work nicely together to avoid some common inner and outer boilerplate, as well as reducing pollution from throwaway variables.</div><div class=""><br class=""></div><div class="">Only one I'm a bit iffy on is the closure; I'm trying to avoid declaring the captured variable externally, but I'm not convinced that having using on its own is clear enough?</div><div class=""><br class=""></div><div class="">Anyway, just an idea!</div><br class=""><div><blockquote type="cite" class=""><div class="">On 6 Jun 2017, at 18:45, Xiaodi Wu 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 dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">On Tue, Jun 6, 2017 at 12:17 PM, Dave Abrahams via swift-evolution<span class="Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span><span class="Apple-converted-space">&nbsp;</span>wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><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=""><br class="">&gt; On Tue, Jun 6, 2017 at 4:05 AM, Michael Savich via swift-evolution &lt;<br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">&gt;<br class="">&gt;&gt; It recently occurred to me how nice it would be to be if we could avoid<br class="">&gt;&gt; declaring variables outside of loops that are only used inside them. I used<br class="">&gt;&gt; google’s site specific search (is that the canon way to search swift-evo?)<br class="">&gt;&gt; and only found one thread about this, (<a href="https://lists.swift.org/" rel="noreferrer" target="_blank" class="">https://lists.swift.org/</a><br class="">&gt;&gt; pipermail/swift-evolution/<wbr class="">Week-of-Mon-20160718/024657.<wbr class="">html) where from<br class="">&gt;&gt; what I could see it got a positive reception.<br class=""><br class=""></span>IMO this is a language design bug of the sort that makes itself<br class="">glaringly obvious after only a few uses of the feature in question.&nbsp; It<br class="">frustrates me that we haven't fixed it yet.<br class=""><span class=""><br class="">on Tue Jun 06 2017, Xiaodi Wu &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">&gt; If I recall correctly, it was discussed during a time when additive<br class="">&gt; proposals were not in scope, so it could not be proposed. Since at the<br class="">&gt; moment we are currently between Swift 4 and Swift 5 evolution, the topic is<br class="">&gt; not in scope either.<br class="">&gt;<br class="">&gt; With respect to the idea itself, Taras's post--which appears to be the last<br class="">&gt; on the subject--is useful to re-consider here:<br class="">&gt;<br class="">&gt;&gt; This is definitively something very useful but it also introduces a<br class="">&gt;&gt; strong asymmetry into Swift statements. In all control-flow statements, the<br class="">&gt;&gt; condition is part of the outer scope. Or, to be more precise, its part of<br class="">&gt;&gt; an intermediate scope between the outer and the inner scope (as you can<br class="">&gt;&gt; declare variables in the condition which are invisible to the outer scope<br class="">&gt;&gt; but visible to the inner scope). Your suggestion essentially moves the<br class="">&gt;&gt; condition of repeat {} while () to the inner scope. I think that the more<br class="">&gt;&gt; complex semantics is not worth the change.<br class=""><br class=""></span>I find that argument unconvincing, personally.&nbsp; Even if the condition in<br class=""><br class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>while condition {...}<br class=""><br class="">was “part of the inner scope,” it would be a distinction without a<br class="">difference, because you can't refer to a local variable before its<br class="">declaration, and the declaration of anything in the inner scope must<br class="">follow the condition.<br class=""><span class=""><br class=""><br class="">&gt;&gt;<br class="">&gt;<br class="">&gt; I recall being initially in favor of the idea myself. However, any sort of<br class="">&gt; change of syntax is a big deal; it will prompt a lot of bikeshedding, and<br class="">&gt; it will require engineering effort to implement that is sorely needed<br class="">&gt; elsewhere. With time, I question whether this idea meets the necessarily<br class="">&gt; high bar for changing syntax; indeed if the motivation is to keep something<br class="">&gt; from the outer scope, it's trivial to make this happen with an outer `do`:<br class="">&gt;<br class="">&gt; ```<br class="">&gt; do {<br class="">&gt;&nbsp; &nbsp;var i = 0<br class="">&gt;&nbsp; &nbsp;repeat {<br class="">&gt;&nbsp; &nbsp; &nbsp;// ...<br class="">&gt;&nbsp; &nbsp;} while i &lt; 42<br class="">&gt; }<br class="">&gt; ```<br class=""><br class=""></span>Don't you gag a little every time you have to add levels of nesting,<br class="">initialize variables twice, or separate declarations from<br class="">initializations as a workaround?&nbsp; I do.<br class=""></blockquote><div class="">&nbsp;</div><div class="">Fair point.</div><div class="">&nbsp;</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;">I'd rather not do this with extra syntax, myself.</blockquote><div class=""><br class=""></div><div class="">Right. My personal opinion today is really that if it's something that requires bikeshedding or extensive engineering, I'd rather not insist on it. But if it's something that can just happen, it's a nice-to-have.</div><div class="">&nbsp;</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;">The change in<br class="">semantics in the rare case where a variable is shadowed inside a loop<br class="">body *and* used in a trailing condition can be handled with warnings and<br class="">migration.<span class="HOEnZb"><font color="#888888" class=""><br class=""></font></span></blockquote><div class=""><br class=""></div><div class="">Right, and migrating those cases *could* reveal and resolve unintentional errors too.</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></div><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></body></html>