<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 style="margin: 0px; line-height: normal; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class=""><font face="HelveticaNeue-Light" size="4" class="">The issue is related to the as? </font></span></div><div style="margin: 0px; line-height: normal; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class=""><font face="HelveticaNeue-Light" size="4" class=""><br class=""></font></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(4, 51, 255); font-variant-ligatures: no-common-ligatures;" class=""><font face="HelveticaNeue-Light" size="4" class="">Try </font></span><font color="#0433ff" face="HelveticaNeue-Light" size="4" class="">this:</font></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class=""><br class=""></span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">if</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> a = </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">try</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures" class="">couldFailButWillNot</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">() {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(a)</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Nick</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div><blockquote type="cite" class=""><div class="">On 21 Jul 2016, at 07:13, Sikhapol Saijit via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Swift Community,<div class=""><br class=""></div><div class=""><br class=""></div><div class="">Yesterday I tried this code:</div><div class=""><br class=""></div><div class=""><font face="Courier" class="">```</font></div><div class=""><div class=""><font face="Courier" class="">func couldFailButWillNot() throws -> Any {</font></div><div class=""><font face="Courier" class=""> return 42</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">if let a = try? couldFailButWillNot() as? Int {</font></div><div class=""><font face="Courier" class=""> print(a)</font></div><div class=""><font face="Courier" class="">}</font></div></div><div class=""><font face="Courier" class="">```</font></div><div class=""><br class=""></div><div class="">And was surprised that the output was <i class="">"Optional(42)”</i> on both Swift 2.2 and Swift 3.</div><div class="">I always have the impression that when a variable is resolved with <font face="Courier" class="">`if let`</font> it will never be optional.</div><div class=""><br class=""></div><div class="">So, with a little investigation, I found out that it happens because <font face="Courier" class="">`as?`</font> has higher precedence than <font face="Courier" class="">`try?`</font> and is evaluated first.</div><div class="">And the whole expression <font face="Courier" class="">`try? couldFailButWillNot() as? Int`</font> evaluated as <i class="">“Optional(Optional(42))”</i>.</div><div class=""><br class=""></div><div class="">Also, I’m surprised that <font face="Courier" class="">`try?`</font> can be used with non-method-call.</div><div class="">This code: <font face="Courier" class="">`print(try? 42)`</font> will print <i class="">“Optional(42)”</i>.</div><div class=""><br class=""></div><div class="">So, the questions are:</div><div class=""><br class=""></div><div class="">1. Is it intentional that <font face="Courier" class="">`try?`</font> can be used with non-method-call and return an optional of the type that follows?</div><div class=""><br class=""></div><div class="">2. Should we design <font face="Courier" class="">`try?`</font> to have higher precedence than <font face="Courier" class="">`as?`</font>. </div><div class="">My intuition tells me that </div><div class=""><font face="Courier" class="">`let a = try? couldFailButWillNot() as? Int`</font></div><div class=""><font face="Courier" class="">and</font> </div><div class=""><font face="Courier" class="">`</font><span style="font-family: Courier;" class="">let a = (try? couldFailButWillNot()) as? Int</span><font face="Courier" class="">`</font> </div><div class="">should be equivalent.</div><div class=""><br class=""></div><div class="">3. Do you think that doubly-nested optional (or multi-level-nested optional) is confusing and should be removed from Swift? (Yes, I’ve seen this blog post [Optionals Case Study: valuesForKeys](<a href="https://developer.apple.com/swift/blog/?id=12" class="">https://developer.apple.com/swift/blog/?id=12</a>))</div><div class="">For me “Optional(nil)” (aka “Optional.Some(Optional.None))”) doesn’t make any sense at all. </div><div class="">Maybe, one of the solution is to always have optional of optional merged into a single level optional? Like Optional(Optional(Optional(42))) should be the merged to Optional(42).</div><div class=""><br class=""></div><div class=""><br class=""><div class="">Thank you</div><div class="">Sikhapol Saijit (Sam)</div><div class="">iOS Developer, Taskworld, Bangkok</div></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>