<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">If it is low cost and people do not come up with regressions/high cost + negative impact scenarios then I would say go full steam ahead. It does address an annoying scenario.<br><br><div id="AppleMailSignature">Sent from my iPhone</div><div><br>On 10 Jun 2017, at 12:04, Gor Gyolchanyan <<a href="mailto:gor@gyolchanyan.com">gor@gyolchanyan.com</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">Not much, I think. The `where` clause already exists, conditional `let` and `var` binding already exists. It'd take loosening up conditional binding rules a bit and expanding the lexical structure to include `let` and `var` bindings in `repeat`.<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 10, 2017, at 2:01 PM, Goffredo Marocchi <<a href="mailto:panajev@gmail.com" class="">panajev@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class="">Quite interesting :), what impact would it have on the compiler?<br class=""><br class=""><div class="">Sent from my iPhone</div><div class=""><br class="">On 10 Jun 2017, at 11:46, Gor Gyolchanyan via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class="">I think a better way of achieving this would be to use the already existing `where` keyword in loops. The way it works right now is as follows:<div class=""><br class=""></div><div class="">let many = [1, 2, 3, 4, 5]</div><div class="">for each in many where each % 2 == 0 {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print("found an even number: \(each)")</div><div class="">}</div><div class=""><br class=""></div><div class="">Unfortunately, unlike all other conditional scopes, `where` does not allow `let` and `var` bindings in it, so I'd suggest we add ability to do that:</div><div class=""><br class=""></div><div class="">let many: [Int?] = [1, 2, nil, 3, 4, nil, 5]</div><div class="">for each in many where let number = each {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print("found a non-nil number: \(number)")</div><div class="">}<div class=""><br class=""></div><div class="">Or, more interestingly:</div><div class=""><br class=""></div><div class="">for each in many where let number = each, number % 2 == 0 {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print("found a non-nil even number: \(number)")</div><div class="">}</div><div class=""><br class=""></div><div class="">And in case of a while loop:</div><div class=""><br class=""></div><div class="">var optional: Int? = 1</div><div class="">while let nonoptional = optional {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if nonoptional >= 10 {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>optional = nil</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>optional = nonoptional + 1</div><div class="">}</div><div class=""><br class=""></div><div class="">But this is only for optional unpacking, so another addition would be to allow any `let` and `var` bindings in conditional scopes without them contributing to the condition itself:</div><div class=""><br class=""></div><div class="">while let a = 0, a < 10 {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>a += 1</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print(a)</div><div class="">}</div><div class=""><br class=""></div><div class="">And finally, allow these bindings in `repeat`:</div><div class=""><br class=""></div><div class="">repeat let a = 0 {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>a += 1</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print(0)</div><div class="">} while a < 10</div><div class=""><br class=""></div><div class="">I think **if** the core team would consider this a worthwhile addition, this would be a less invasive and more intuitive way of achieving what you want.</div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jun 10, 2017, at 1:31 PM, Haravikk 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=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Not sure if my e-mail didn't go through or if discussion just fizzled out; one other benefit if we ever move to a proper message board is we might gain the ability to bump topics. Anyway, I'll resend my message just in case:</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div 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=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><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>} while (i < 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 < 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 < 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) -> 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 < 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></div></div></div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></div></blockquote></div><br class=""></div></blockquote></body></html>