<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 <<a href="mailto:jtbandes@gmail.com" class="">jtbandes@gmail.com</a>> 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=""> if let value = getValue() {<br class=""> // do something with value</div><div class=""> }</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> 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=""> if let value1 = somethingOptional(),</div><div class=""> let value2 = somethingNonOptional()</div><div class=""> {</div><div class=""> // should this be allowed?</div><div class=""> }</div><div class=""><br class=""></div><div class=""><div class=""><br class=""> if let value1 = somethingNonOptional(),</div><div class=""> let value2 = somethingOptional()</div><div class=""> {</div><div class=""> // How about this?</div> }</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=""> <b class="">do let</b> value = somethingNonOptional() {</div><div class=""> // use value</div><div class=""> } // 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"> if let value1 = somethingOptional(),</div><div class="gmail_extra"> <b class="">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 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=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></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="">
> Le 7 janv. 2016 à 07:40, Russ Bishop via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> a écrit :<br class="">
><br class="">
> 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="">
><br class="">
> 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="">
><br class="">
><br class="">
> Does anyone have strong thoughts about this? I tried searching the evolution repo and the mailing list and didn’t see anything.<br class="">
><br class="">
><br class="">
> —russ<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="">
<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>