<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="">That would be fine for me, as long as Xcode suggests adding the missing `do`, or replacing `if` with `do` when I forget one!<div class=""><br class=""></div><div class="">Gwendal<br class=""><div class=""><br class=""></div><div class=""><div class=""><div class=""><div><blockquote type="cite" class=""><div class="">Le 7 janv. 2016 à 19:59, Jacob Bandes-Storch &lt;<a href="mailto:jtbandes@gmail.com" class="">jtbandes@gmail.com</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Well, I don't think it should be universally allowed.<div class=""><br class=""></div><div class="">Consider:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; if let value = getValue() {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; // do something with value</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">If getValue() returns an optional, this is fine. But if it's non-optional, then you've introduced an "if" when there really is <b class="">no</b>&nbsp;conditional control flow happening. This should still be a compiler error.</div><div class=""><br class=""></div><div class="">It gets trickier when you mix optional and non-optional values:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; if let value1 = somethingOptional(),</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; let value2 = somethingNonOptional()</div><div class="">&nbsp; &nbsp; {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; // should this be allowed?</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class=""><div class=""><br class="">&nbsp; &nbsp; if let value1 = somethingNonOptional(),</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; let value2 = somethingOptional()</div><div class="">&nbsp; &nbsp; {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; // How about this?</div>&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">Here's an alternative idea: allow non-optional bindings in "do" blocks:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; <b class="">do let</b> value = somethingNonOptional() {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; // use value</div><div class="">&nbsp; &nbsp; } // value is out of scope now</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">And perhaps you could combine them with if-statements:</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">&nbsp; &nbsp; if let value1 = somethingOptional(),</div><div class="gmail_extra">&nbsp; &nbsp; &nbsp; &nbsp; <b class="">do let</b> value2 = somethingNonOptional()</div><div class="gmail_extra">&nbsp; &nbsp; &nbsp; &nbsp; where value2 &lt; value1</div><div class="gmail_extra">&nbsp; &nbsp; {</div><div class="gmail_extra">&nbsp; &nbsp; &nbsp; &nbsp; // use the values</div><div class="gmail_extra">&nbsp; &nbsp; }</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class=""><div dir="ltr" class=""><div class="">Jacob Bandes-Storch<br class=""></div></div></div></div>
<br class=""><div class="gmail_quote">On Thu, Jan 7, 2016 at 10:41 AM, Gwendal Roué <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><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">+1.<br class="">
<br class="">
`if let` does two things: 1. unwrap the optional, 2. define a new variable, scoped to the `if` statement.<br class="">
<br class="">
Sometimes we don’t need the unwrapping, but we’d like the new, temporary, scoped variable.<br class="">
<br class="">
Gwendal<br class="">
<div class=""><div class=""><br class="">
<br class="">
&gt; Le 7 janv. 2016 à 07:40, Russ Bishop via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; a écrit :<br class="">
&gt;<br class="">
&gt; I often want to bind variables to a specific scope, or perhaps I have three optionals I want to bind and one non-optional. (Often I need that non-optional value as part of a where condition check but not being able to bind it means I leak the variable to the outer scope).<br class="">
&gt;<br class="">
&gt; Not being able to bind non-optionals breaks the flow of the code and forces me to make a minor context switch when it really doesn’t matter, nor does it aid code clarity.<br class="">
&gt;<br class="">
&gt;<br class="">
&gt; Does anyone have strong thoughts about this? I tried searching the evolution repo and the mailing list and didn’t see anything.<br class="">
&gt;<br class="">
&gt;<br class="">
&gt; —russ<br class="">
&gt; _______________________________________________<br class="">
&gt; swift-evolution mailing list<br class="">
&gt; <a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></div></div></div></div></body></html>