<div dir="ltr">Could take a page out of Kotlin's book:<div><br></div><div>x.let { foo(it) } // default argument name that isn't $0</div><div><br></div><div>IMO, this syntax looks better as a one liner than "if x? { foo(x) }".</div><div><br></div><div>The edge case here, with foo and bar being optional-returning functions, becomes:</div><div>(foo(42), bar(43)).let { x, y in foo(x, y) } // default argument "it" here is a tuple so you can't pass it directly to the function, therefore you have to name x and y explicitly </div><div><br></div><div>In which case it should be abundantly clear to the reader that there is NO short-circuiting and both foo and bar are evaluated as a tuple before the "let" function is called on that tuple. </div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Sep 25, 2016 at 3:24 AM Justin Jia via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></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"><div><blockquote type="cite"><div>On Sep 24, 2016, at 7:31 PM, Dany St-Amant <<a href="mailto:dsa.mls@icloud.com" target="_blank">dsa.mls@icloud.com</a>> wrote:</div><br><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"><span></span></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"><div>Reading some old threads...</div><div><br></div><div>Le 16 août 2016 à 15:12, Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> a écrit :<br><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Aug 16, 2016 at 12:14 PM, Justin Jia<span> </span><span dir="ltr"><<a href="mailto:justin.jia.developer@gmail.com" target="_blank">justin.jia.developer@gmail.com</a>></span><span> </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"><div dir="auto"><div></div><div>I was trying to argue with reason. You kept stating your *opinion*. You still didn't explain *why*. For example, why "if statements can and function calls can't" and why "this severely violates user expectations"?</div></div></blockquote><div><br></div><div>These were not meant as expressions of opinion. Currently, in Swift, `if` statements can short circuit and function calls cannot. Your proposal would introduce short-circuiting where currently there can be none, and thus it would severely violate user expectations.</div></div></div></div></blockquote><div><br></div><span style="background-color:rgba(255,255,255,0)">Function calls can currently be short-circuited... if there's try and throw involved.</span><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">let z = try g(f1(a), f2(b), f3(c)) // Must be within do {} catch {}</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">Assuming f1(), f2(), f3() can all throws, f2() is only called if f1() did not throw, and f3() if neither f1() nor f2() threw. The behavior is/seems to be: Pure left to right execution order regardless of throwing ability.</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">If g() doesn't throw, the above (with exact same behavior) can be written more verbosely and explicitly as:</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">let y = g(try f1(a), try f2(b), try f3(c)) // Must be within do {} catch {}</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">Yet another way to do the call is:</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">let x = try? g(f1(a), f2(b), f3(c)) // z is nil on throws</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">So implementing something like what Justin is asking should fit within Swift, as long as it follows the try short-circuit logic. However, the use of a trailing question-mark to provide this functionality is not explicit enough to my taste (and to my weakening eyesight). Could we reuse let but with a question-mark? Or maybe guard.</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>Good point! Edge cases always exist.</div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><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"><div><span style="background-color:rgba(255,255,255,0)">let z = g(let? f1(a), let? f2(b), let? f3(c))</span></div><div><span style="background-color:rgba(255,255,255,0)">// z is nil on fX() == nil, otherwise Optional(g()) just like try? wrapping</span></div><div><span style="background-color:rgba(255,255,255,0)">// left to right short-circuit à la let z = try?</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>This sounds like a good idea. Moving the keyword to the front is more explicit. But `let? f1(a)` seems a little bit weird to me.</div><div><br></div><div>What about `let x = if? foo(x, y)` But… should we introduce `if!` as well?</div><div><br></div><div>```</div><div>func foo(x: Any, y: Any) { … }</div><div><br></div><div>let x: Any? = nil</div><div>let y: Any? = nil</div><div>if? foo(x, y) // If x and y both can be unwrapped, execute foo().</div><div>if? foo(x?, y?) // Or if we want to be a little bit more clear, specify which argument could be optional</div><div>```</div><div><br></div><div>```</div><div>if! foo(x, y) // Will crash if nil is found.</div><div>if! foo(x!, y!) // To be more clear</div><div>```</div><div><br></div><div>Backward compatibility:</div><div><br></div><div>```</div><div>foo(x!, y!) // warning: add if! before the function</div><div>```</div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><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"><div><span style="background-color:rgba(255,255,255,0)">Though, this may ask for also supporting something like:</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">let x = g(let? try? f1(a), let? try? f2(b), let? try? f3(c))</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">This allow selective discard of throws, instead of a discard all of a plain 'let x = try? ...'</span></div><div></div><div><br></div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>Yes.</div></div></div><div style="word-wrap:break-word"><div><div><br></div><blockquote type="cite"><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"><div><span style="background-color:rgba(255,255,255,0)">Dany</span></div><div><br></div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><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"><div dir="auto">... snip ...</div></blockquote><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"><div dir="auto"><div><blockquote type="cite"><div class="gmail_quote"><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"><div style="word-wrap:break-word"><blockquote type="cite"><div class="gmail_quote"><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"><div style="word-wrap:break-word"><br><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><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"><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On Aug 16, 2016, at 1:16 AM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>> wrote:</div><br><div><div dir="ltr" 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"><div class="gmail_extra"><div class="gmail_quote">On Mon, Aug 15, 2016 at 12:07 PM, Xiaodi Wu<span> </span><span dir="ltr"><<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>></span><span> </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"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span>On Mon, Aug 15, 2016 at 11:43 AM, Justin Jia<span> </span><span dir="ltr"><<a href="mailto:justin.jia.developer@gmail.com" target="_blank">justin.jia.developer@gmail.com</a>></span><span> </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"><div style="word-wrap:break-word"><div><div>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></div></blockquote><div>.. snip ...</div></span><span><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"><div style="word-wrap:break-word"><div><div><br></div><div>Back to the original topic.</div><div><br></div><div>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><br></div><div>-</div><div><br></div><div>Below is the reason why I think this feature is important (quoted from another email).</div><div><br></div><div>The problem with `if let` is you need to call the function inside { }.</div><div><br></div><div>```</div><div>/* code 1 */</div><span><div>if let x = x, let y = y {</div></span><div> <span> </span>/* code 2, depends on x and y to be non-optional */</div><div> <span> </span>let z = foo(x, y)</div><div> <span> </span>if let z = z {</div><div> <span> </span>bar(z)</div><div> <span> </span>}</div><div> <span> </span>/* code 3, depends on x and y to be non-optional */</div><div>}</div><div>/* code 4 */</div><div>```</div><div><br></div><div>I can't use `guard` for this situation because guard will force me to leave the entire function.</div></div></div></blockquote></span></div></div></div></blockquote><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"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><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"><div style="word-wrap:break-word"><div><div><br></div><div>```</div><div>/* code 1 */</div><span><div>guard let x = x, y = y else { return }</div></span><div>/* code 2, depends on x and y to be non-optional */</div><span><div>guard let z = foo(x, y) else { return }</div><div>bar(z)</div></span><div>/* code 3, depends on x and y to be non-optional */ <- This won't execute if z is nil</div><div>/* code 4 */ <- This won't execute if x, y or z is nil</div><span><div>```</div></span></div></div></blockquote><div><br></div></span><div>Then surround it with a do block.</div><div><br></div><div>```</div><div>out: do {</div><div> <span> </span>guard foo else { break out }</div><div> <span> </span>guard bar else { break out }</div><div> <span> </span>/* other code */</div><div><div><div>}</div><div>```</div></div></div></div></div></div></blockquote><div><br></div><div>Or, more idiomatically, since your use case is that you want /* code 4 */ to be executed no matter what, while everything else depends on x and y not being nil:</div><div><br></div><div>```</div><div>defer { /* code 4 */ }</div><div>guard let x = x, let y = y else { return }</div><div>/* code 2 */</div><div>/* code 3 */</div><div>```</div><div><br></div><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"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><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"><div style="word-wrap:break-word"><div><span><div><br></div><div>What I really want is some like this:</div><div><br></div><div>```</div><div>/ * code 1 */</div></span><span><div>let z = foo(x?, y?)</div></span><div>/* code 2, depends on x and y to be non-optional, use x? and y? */</div><div>bar(z?)</div><div>/* code 3, depends on x and y to be non-optional, use x? and y? */</div><div>/* code 4 */</div><div>```</div><div>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><br></div><div>Sincerely,</div><div>Justin</div></div><div><div><div><br></div><div><br></div><div><br></div><br><div><blockquote type="cite"><div>On Aug 15, 2016, at 11:41 PM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>> wrote:</div><br><div><div style="white-space:pre-wrap">What do you mean, limited to variables? What about a computed property? You will have the same problem.<br><br>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></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Aug 15, 2016 at 04:56 Justin Jia <<a href="mailto:justin.jia.developer@gmail.com" target="_blank">justin.jia.developer@gmail.com</a>> wrote:<br></div><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"><div dir="auto"><div></div><div style="direction:inherit">IMO I don't this bar should be evaluated unless we decide if let can accept non-optional values. </div><div style="direction:inherit"><br></div><div style="direction:inherit">Actually, what if we allow if let to accept non-optional values?</div><div style="direction:inherit"><br></div><div style="direction:inherit"><span style="background-color:rgba(255,255,255,0)">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. </span></div><div style="direction:inherit"><br></div><div style="direction:inherit">Btw, do you think this is still something nice to have if we limit this syntax to only variables?</div></div><div dir="auto"><div><div style="direction:inherit"><br></div>On Aug 15, 2016, at 4:59 PM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">On Mon, Aug 15, 2016 at 3:55 AM, Xiaodi Wu<span> </span><span dir="ltr"><<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>></span><span> </span>wrote:<br><div class="gmail_extra"><div class="gmail_quote"><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"><div dir="ltr"><div><div>On Mon, Aug 15, 2016 at 3:25 AM, Justin Jia via swift-evolution<span> </span><span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span><span> </span>wrote:<br></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><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"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On Aug 15, 2016, at 4:09 PM, Charlie Monroe <<a href="mailto:charlie@charliemonroe.net" target="_blank">charlie@charliemonroe.net</a>> wrote:</div><br><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">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, </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"><br></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">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"><br></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">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 </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"><br></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">foo(bar(42)?, 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"><br></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">evaluate both arguments even if the first one is nil, making it incosistent with the rest of the language?</div></div></blockquote></div><br><div>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><br></div><div>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><div><br></div><div>If they want to opt-out this behavior, they can always write:</div><div><br></div><div>```</div><div>let x = bar(42)</div><div>let y = baz(42)</div></span><div>foo(x?, y?)</div><div>```</div></div></blockquote><div><br></div></div></div><div>Well, that was just the easy part. Now, suppose bar is the function that isn't optional.</div><div><br></div><div>```</div><div>foo(bar(42), baz(42)?)</div><div>```</div><div><br></div><div>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><br></div><div>s/yes/no/</div><div> </div><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"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>If short-circuiting works left-to-right, then the answer is no.</div></div></div></div></blockquote><div><br></div><div>s/no/yes/ </div><div><br></div><div>(See? Confusing.)</div><div> <br></div><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"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>This is very confusing, and there is no good intuitive answer. <br></div><span><div><br></div><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"><div style="word-wrap:break-word"><div></div></div><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></blockquote></span></div></div></div></blockquote></div></div></div></div></blockquote></div></blockquote></div></div></blockquote></div></div></div></div></blockquote></div></div></div></div></blockquote></div></div></div></div></blockquote></div><br></div><br></blockquote></div><br></div></div></blockquote></div></blockquote></div></blockquote></div></blockquote></div></blockquote></div></div></blockquote></div><br></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span></div></blockquote></div></div></blockquote></div></div>_______________________________________________<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>
</blockquote></div>