<div dir="ltr">Well, I don&#39;t think it should be universally allowed.<div><br></div><div>Consider:</div><div><br></div><div>    if let value = getValue() {<br>        // do something with value</div><div>    }</div><div><br></div><div>If getValue() returns an optional, this is fine. But if it&#39;s non-optional, then you&#39;ve introduced an &quot;if&quot; when there really is <b>no</b> conditional control flow happening. This should still be a compiler error.</div><div><br></div><div>It gets trickier when you mix optional and non-optional values:</div><div><br></div><div>    if let value1 = somethingOptional(),</div><div>        let value2 = somethingNonOptional()</div><div>    {</div><div>        // should this be allowed?</div><div>    }</div><div><br></div><div><div><br>    if let value1 = somethingNonOptional(),</div><div>        let value2 = somethingOptional()</div><div>    {</div><div>        // How about this?</div>    }</div><div><br></div><div>Here&#39;s an alternative idea: allow non-optional bindings in &quot;do&quot; blocks:</div><div><br></div><div>    <b>do let</b> value = somethingNonOptional() {</div><div>        // use value</div><div>    } // value is out of scope now</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">And perhaps you could combine them with if-statements:</div><div class="gmail_extra"><br></div><div class="gmail_extra">    if let value1 = somethingOptional(),</div><div class="gmail_extra">        <b>do let</b> value2 = somethingNonOptional()</div><div class="gmail_extra">        where value2 &lt; value1</div><div class="gmail_extra">    {</div><div class="gmail_extra">        // use the values</div><div class="gmail_extra">    }</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><div>Jacob Bandes-Storch<br></div></div></div></div>
<br><div class="gmail_quote">On Thu, Jan 7, 2016 at 10:41 AM, Gwendal Roué <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">+1.<br>
<br>
`if let` does two things: 1. unwrap the optional, 2. define a new variable, scoped to the `if` statement.<br>
<br>
Sometimes we don’t need the unwrapping, but we’d like the new, temporary, scoped variable.<br>
<br>
Gwendal<br>
<div><div><br>
<br>
&gt; Le 7 janv. 2016 à 07:40, Russ Bishop via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; a écrit :<br>
&gt;<br>
&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>
&gt;<br>
&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>
&gt;<br>
&gt;<br>
&gt; Does anyone have strong thoughts about this? I tried searching the evolution repo and the mailing list and didn’t see anything.<br>
&gt;<br>
&gt;<br>
&gt; —russ<br>
&gt; _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
&gt; <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>
_______________________________________________<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>
</div></div></blockquote></div><br></div></div>