<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=""><div class=""><div class="">I believe the core team has considered 99% of the ideas in the mailing list in the past, but it doesn’t mean we can’t discuss it, right?</div><div class=""><br class=""></div><div class="">Assuming we have the following declaration:</div><div class=""><br class=""></div><div class="">```</div><div class="">func foo(a: Int, b: Int?, c: Int, d: Int?) -&gt; Int</div><div class="">```</div><div class=""><br class=""></div><div class="">For this:</div><div class=""><br class=""></div><div class="">```</div><div class="">let z = foo(a: f1(), b: f2()?, c: f3(), d: f4()?) // z becomes optional</div><div class="">```</div><div class=""><br class=""></div><div class="">We have a few different “possible solutions”:</div><div class=""><br class=""></div><div class="">1. Short-circuiting from left to right. This is equivalent to:</div><div class=""><br class=""></div><div class="">```</div><div class="">var z: Int? = nil</div><div class="">let a = f1()</div><div class="">guard let b = f2() else { return }</div><div class="">let c = f3()</div><div class="">guard let d = f4() else { return }</div><div class="">z = foo(a: a, b: b, c: c, d: d)</div><div class="">```</div><div class=""><br class=""></div><div class="">2. Short-circuiting from left to right for optionals. Then evaluate non-optional parameters. This is equivalent to:</div><div class=""><br class=""></div><div class="">```</div><div class="">var z: Int? = nil</div><div class="">guard let b = f2() else { return }</div><div class="">guard let d = f4() else { return }</div><div class="">let a = f1()</div><div class="">let c = f3()</div><div class="">z = foo(a: a, b: b, c: c, d: d)</div><div class="">```</div><div class=""><br class=""></div><div class="">3. Do not short-circuiting.</div><div class=""><br class=""></div><div class="">```</div><div class="">var z: Int? = nil</div><div class="">let a = f1()</div><div class="">let optionalB = f2()</div><div class="">let c = f3()</div><div class="">let optionalD = f4()</div><div class="">guard let b = optionalB else { return }</div><div class="">guard let d = optionalD else { return }</div><div class="">z = foo(a: a, b: b, c: c, d: d)</div><div class="">```</div><div class=""><br class=""></div><div class="">Like I said before, I agree that there is no intuitive solution to this problem. However, I'm still not convinced that this feature is *not important*.</div><div class=""><br class=""></div><div class="">Thank you for pointing out the problem to me. I didn't notice it at the time I wrote my first email. I really appreciate that. However, instead of saying I don't know which is the best solution so let's assume the core team made the right decision, we should discuss whether 1, 2, 3 is the best solution. Or you can convince me we don't *need* this feature.</div><div class=""><br class=""></div><div class="">Back to the original topic.</div><div class=""><br class=""></div><div class="">I spent some time thinking and changed my mind again. I think solution 1 is most reasonable. It is consistent with if statements. Instead of treating it as sugar for `if let`, we can treat it as sugar for `guard`, which is much easy to understand and remember.</div><div class=""><br class=""></div><div class="">-</div><div class=""><br class=""></div><div class="">Below is the reason why I think this feature is important (quoted from another email).</div><div class=""><br class=""></div><div class="">The problem with `if let` is you need to call the function inside { }.</div><div class=""><br class=""></div><div class="">```</div><div class="">/* code 1 */</div><div class="">if let x = x, let y = y {</div><div class="">&nbsp; &nbsp; /* code 2, depends on x and y to be non-optional */</div><div class="">&nbsp; &nbsp; let z = foo(x, y)</div><div class="">&nbsp; &nbsp; if let z = z {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; bar(z)</div><div class="">&nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; /* code 3, depends on x and y to be non-optional */</div><div class="">}</div><div class="">/* code 4 */</div><div class="">```</div><div class=""><br class=""></div><div class="">I can't use `guard` for this situation because guard will force me to leave the entire function.</div><div class=""><br class=""></div><div class="">```</div><div class="">/* code 1 */</div><div class="">guard let x = x, y = y else { return }</div><div class="">/* code 2, depends on x and y to be non-optional */</div><div class="">guard let z = foo(x, y) else { return }</div><div class="">bar(z)</div><div class="">/* code 3, depends on x and y to be non-optional */ &lt;- This won't execute if z is nil</div><div class="">/* code 4 */ &lt;- This won't execute if x, y or z is nil</div><div class="">```</div><div class=""><br class=""></div><div class="">What I really want is some like this:</div><div class=""><br class=""></div><div class="">```</div><div class="">/ * code 1 */</div><div class="">let z = foo(x?, y?)</div><div class="">/* code 2, depends on x and y to be non-optional, use x? and y? */</div><div class="">bar(z?)</div><div class="">/* code 3, depends on x and y to be non-optional, use x? and y? */</div><div class="">/* code 4 */</div><div class="">```</div><div class="">This is much easier to read. Sometimes people choose to use `guard` to avoid `{ }`, which usually lead to code could easily get wrong (like the second example).</div><div class=""><br class=""></div><div class="">Sincerely,</div><div class="">Justin</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 15, 2016, at 11:41 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="white-space:pre-wrap" class="">What do you mean, limited to variables? What about a computed property? You will have the same problem.<br class=""><br class="">I'm not sure where you want to go with this, given that the core team has considered the same idea in the past and found these issues to have no good solution.<br class=""></div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Mon, Aug 15, 2016 at 04:56 Justin Jia &lt;<a href="mailto:justin.jia.developer@gmail.com" class="">justin.jia.developer@gmail.com</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" class=""><div class=""></div><div style="direction:inherit" class="">IMO I don't this bar should be evaluated unless we decide if let can accept non-optional values.&nbsp;</div><div style="direction:inherit" class=""><br class=""></div><div style="direction:inherit" class="">Actually, what if we allow if let to accept non-optional values?</div><div style="direction:inherit" class=""><br class=""></div><div style="direction:inherit" class=""><span style="background-color:rgba(255,255,255,0)" class="">I agree this is confusing at the beginning. But people who are not familiar with the detail design can avoid this situation easily. People who are familiar with the design can adopt it quickly. Sometimes, this is unavoidable.&nbsp;</span></div><div style="direction:inherit" class=""><br class=""></div><div style="direction:inherit" class="">Btw, do you think this is still something nice to have if we limit this syntax to only variables?</div></div><div dir="auto" class=""><div class=""><div style="direction:inherit" class=""><br class=""></div>On Aug 15, 2016, at 4:59 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank" class="">xiaodi.wu@gmail.com</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">On Mon, Aug 15, 2016 at 3:55 AM, Xiaodi Wu <span dir="ltr" class="">&lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank" class="">xiaodi.wu@gmail.com</a>&gt;</span> wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class=""><div class="">On Mon, Aug 15, 2016 at 3:25 AM, Justin Jia via swift-evolution <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=""></div></div><div class="gmail_extra"><div class="gmail_quote"><div class=""><div class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Aug 15, 2016, at 4:09 PM, Charlie Monroe &lt;<a href="mailto:charlie@charliemonroe.net" target="_blank" class="">charlie@charliemonroe.net</a>&gt; wrote:</div><br class=""><div class=""><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">The example above was to better demonstrate the problem with *when* to evaluate the latter argument. Why should both arguments be evaluated *before* the if statement? If both calls return Optionals,&nbsp;</div><span class=""><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">if let x = bar(42), y = baz(42) { ... }</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div></span><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">is how would I write it without the suggested syntax - baz(42) will *not* be evaluated if bar(42) returns nil. Which bears a question why would&nbsp;</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">foo(bar(42)?, baz(42)?)&nbsp;</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">evaluate both arguments even if the first one is nil, making it incosistent with the rest of the language?</div></div></blockquote></div><br class=""><div class="">I see your point. I understand that maybe 1/2 of the people think we should evaluate both arguments and 1/2 of the people think we should only evaluate the first argument.</div><div class=""><br class=""></div><div class="">I changed my idea a little bit. Now I think you are right. We should only evaluate the first argument in your example. It’s not only because of inconsistent, but also because the language should at least provide a way to “short-circuit” to rest of the arguments.</div><span class=""><div class=""><br class=""></div><div class="">If they want to opt-out this behavior, they can always write:</div><div class=""><br class=""></div><div class="">```</div><div class="">let x = bar(42)</div><div class="">let y = baz(42)</div></span><div class="">foo(x?, y?)</div><div class="">```</div></div></blockquote><div class=""><br class=""></div></div></div><div class="">Well, that was just the easy part. Now, suppose bar is the function that isn't optional.</div><div class=""><br class=""></div><div class="">```</div><div class="">foo(bar(42), baz(42)?)</div><div class="">```</div><div class=""><br class=""></div><div class="">Is bar evaluated if baz returns nil? If you want this syntax to be sugar for if let, then the answer is yes.</div></div></div></div></blockquote><div class=""><br class=""></div><div class="">s/yes/no/</div><div class="">&nbsp;</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">If short-circuiting works left-to-right, then the answer is no.</div></div></div></div></blockquote><div class=""><br class=""></div><div class="">s/no/yes/&nbsp;</div><div class=""><br class=""></div><div class="">(See? Confusing.)</div><div class="">&nbsp;<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">This is very confusing, and there is no good intuitive answer.&nbsp;<br class=""></div><span class=""><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""></div></div><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=""></blockquote></span></div><br class=""></div></div>
</blockquote></div><br class=""></div></div>
</div></blockquote></div></blockquote></div>
</div></blockquote></div><br class=""></body></html>