<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 08:02, Justin Jia via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div 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?</div></div></blockquote><br class=""></div><div>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.</div><div><br class=""></div><div>There may be some alternatives though, for example, what about a shorthand for the conditional like so:</div><div><br class=""></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if let x? { foo.bar(x: x) }</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if x? { foo.bar(x: x) } // even shorter?</font></div><div><br class=""></div><div>But in general, I think it's best to be explicit about the entire statement being optional, which the conditional does but a postfix on a variable doesn't to the same degree.</div></body></html>