<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On 15 Aug 2016, at 21:27, 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 dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On Mon, Aug 15, 2016 at 1:57 PM, Haravikk 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=""><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=""><span class=""><blockquote type="cite" class=""><div class="">On 15 Aug 2016, at 13:44, Tim Vermeulen &lt;<a href="mailto:tvermeulen@me.com" target="_blank" class="">tvermeulen@me.com</a>&gt; wrote:</div><br class=""><div class=""><div class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On 15 Aug 2016, at 08:02, Justin Jia via swift-evolution&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-<wbr class="">evolution@swift.org</a>(<a href="mailto:swift-evolution@swift.org" target="_blank" class="">mailto:<wbr class="">swift-evolution@swift.org</a>)&gt;<wbr class="">wrote:<br class="">Hi!<br class=""><br class="">I don’t know if this has came up before. I tried to search though the mailing list but didn’t find any related threads.<br class=""><br class="">This is purely a syntactic thing (which I know it’s the lowest priority for Swift 4), but I think it’s an important one.<br class=""><br class="">Let’s say we have a struct with a function:<br class=""><br class="">```<br class="">struct Foo {<br class="">func bar(x: Int)<br class="">}<br class="">```<br class=""><br class="">We can use optionals:<br class=""><br class="">```<br class="">let foo: Foo? = nil<br class="">let x = 1<br class="">foo!.bar(x: x) // Able to compile, but will cause runtime error<br class="">foo?.bar(x: x) // Able to compile, and won't cause runtime error<br class="">```<br class=""><br class="">However:<br class=""><br class="">```<br class="">let foo = Foo()<br class="">let x: Int? = nil<br class="">foo.bar(x: x!) // Able to compile, but will cause runtime error<br class="">foo.bar(x: x?) // Won't compile<br class="">```<br class=""><br class="">I propose that we should allow `foo.bar(x: x?)`, which should be equivalent to:<br class=""><br class="">```<br class="">if let x = x {<br class="">foo.bar(x: x)<br class="">}<br class="">```<br class=""><br class="">What do you think?<br class=""></blockquote>I like the intent behind this, but personally I think it's not clear enough. For me, putting the statement in a conditional as you've shown is the better solution, as it's a lot clearer exactly what's going on. Putting a question mark on a variable makes it look like something specific to that variable, rather than preventing the entire statement from executing.<br class=""></blockquote><br class="">I get where you’re coming from, but how would people react if optional chaining wasn’t in the language yet and someone proposed it now? I know it’s not strictly the same thing, but it’s still a single question mark that prevents the whole statement from being executed. I think it would be met with a lot of resistance from people saying that being more explicit with `if let` is the way to go.<br class=""></div></div></blockquote><div class=""><br class=""></div></span><div class="">True, but with optional chaining the position of the question mark makes it a lot more clear where it stops, whereas in this proposal the question mark seems a bit less intuitive since it's within the parenthesis yet affecting the statement outside of it.</div><span class=""><br class=""><blockquote type="cite" class=""><div class=""><div class=""><blockquote type="cite" class="">There may be some alternatives though, for example, what about a shorthand for the conditional like so:<br class=""><br class="">if let x? { foo.bar(x: x) }<br class="">if x? { foo.bar(x: x) } // even shorter?<br class=""></blockquote><br class="">The alternatives you’ve come up with would only work if foo.bar doesn’t return anything. If it does return something, and you want to assign it to a variable, you have to declare the variable beforehand and it just becomes ugly. It’s then probably a better idea to use map/flatmap:</div></div></blockquote></span></div><br class=""><div class="">Hmm, what about something involving the where keyword? Something like:</div><div class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class=""><div class=""><font face="Monaco" class="">let value = foo.bar(x: x) where x?</font></div></blockquote><br class=""><div class="">Some people have queried the ability to use where in assignments before, as another way to have a statement be nil if a condition isn't met, but in this case the condition is that x is unwrapped (thus valid for the call). This basically lets you use it like a "retroactive" conditional, it'd be nice to get the same x? behaviour on for loops anyway (letting you unwrap values that way, and maybe test them too).</div></div></blockquote><div class=""><br class=""></div><div class="">`let value = (x == nil) ? nil : foo.bar(x: x)` isn't so bad, is it?</div></div></div></div></div></blockquote><div><br class=""></div><div>In my opinion, it is. And you’d even need to write `foo.bar(x: x!)`, right?</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">You could even write a custom operator to sugar it.</div><div class="">&nbsp;</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br class="">______________________________<wbr class="">_________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" 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/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></body></html>