<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>I think in the second case the compiler could give you a warning.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature"><br>Sent from my iPhone</div><div><br>On 7 Jan 2016, at 18:59, Jacob Bandes-Storch via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Well, I don'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's non-optional, then you've introduced an "if" 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's an alternative idea: allow non-optional bindings in "do" 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 < 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"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></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>
> Le 7 janv. 2016 à 07:40, Russ Bishop via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> a écrit :<br>
><br>
> 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>
><br>
> 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>
><br>
><br>
> Does anyone have strong thoughts about this? I tried searching the evolution repo and the mailing list and didn’t see anything.<br>
><br>
><br>
> —russ<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>
<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>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=xV0JY-2FdZMnUMvSFtZnLiBPRTDDOSQf3-2FpH33HYOlBxGI7NdFJBxImECTFKdR84PEYbHn3yPxacuZI-2FXmE0vT2YnRQ9e2GAlxXyfNwZFXJYFCzt4pe6mM0ZMZ4a-2B7lkg99LdbtWh9pWYKhsw-2BgsxRccuRv4zWPuj-2FL13ION-2BFk5Rgg7lB5WBlEwinNobPzpNFyc0SDWZUMEnPks32bYGLIoGJQSmdWxVC7ePiloLMzqI-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;">
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>