<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="">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></body></html>