<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 all,<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=""><div class=""><div class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> couldFailButWillNot() </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">42</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" 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: #ba2da2" 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><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; 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: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(a)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div></div></div></div><div class="">And was surprised that the output was <b style="font-family: Menlo; font-size: 11px;" class="">Optional(42)</b> on both Swift 2 and Swift 3.</div><div class="">I always have the impression that when a variable is resolved with <font class=""><span style="font-family: Menlo; color: rgb(186, 45, 162); font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">if</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; color: rgb(186, 45, 162); font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">let</span><span style="color: rgb(186, 45, 162); font-variant-ligatures: no-common-ligatures;" class=""> </span>it will never be optional.</font></div><div class=""><br class=""></div><div class="">So, with a little investigation, I found out that it happens because <span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">as</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">?</span> has higher precedence than <span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">try</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">?</span> and is evaluated first.</div><div class="">And the whole expression <font face="Menlo" style="font-size: 11px;" class="">`<span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">try</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? couldFailButWillNot() </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">as</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Int`</span></font> evaluated as <span class="" style="font-size: 11px;"><font face="Menlo" class=""><b class="">Optional(Optional(42))</b></font></span>.</div><div class=""><br class=""></div><div class="">Also, I’m surprised that <span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">try</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">?</span> can be used with non-method-call.</div><div class="">This code: <span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">`print(</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">try</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">? </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">42</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">)`</span> will print <b style="font-family: Menlo; font-size: 11px;" class="">Optional(42)</b>.</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 <span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">try</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">?</span> can be used with a "non-method-call" and return an optional of the type that follows?</div><div class=""><br class=""></div><div class="">2. Should we design <span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">try</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">?</span> to have higher precedence than <span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">as</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">? </span>or any operators at all?</div><div class="">My intuition tells me that </div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> a = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">try</span><span style="font-variant-ligatures: no-common-ligatures" class="">? couldFailButWillNot() </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span></div></div><div class="">should be equivalent to</div><div class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">let</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""> a = (</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">try</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">? couldFailButWillNot()) </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">as</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">? </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Int</span> </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 <a href="https://developer.apple.com/swift/blog/?id=12" class="">Optionals Case Study: valuesForKeys</a>).</div><div class="">For me <font face="Menlo" style="font-size: 11px;" class=""><b class="">Optional(nil)</b></font> (aka <font face="Menlo" style="font-size: 11px;" class=""><b class="">Optional.Some(Optional.None))</b></font>) doesn’t make much sense. </div><div class="">Maybe, one of the solution is to always have optional of optional merged into a single level optional? Like <font face="Menlo" style="font-size: 11px;" class=""><b class="">Optional(Optional(Optional(42)))</b></font> should be the merged to and evaluated as <font face="Menlo" style="font-size: 11px;" class=""><b class="">Optional(42)</b></font>.</div><div class=""><br class=""></div><div class="">BTW, the code above is merely for a demonstration. The actual code was more of something like this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> parse(JSON: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Data</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// …</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> dict = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">try</span><span style="font-variant-ligatures: no-common-ligatures" class="">? parse(JSON: json) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures" class="">? [</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures" class="">] {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// assume dict is a valid [String: Any] dictionary</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// …</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">I’m new to this mailing list so I’m not sure if this belongs here. I’m sorry in advance if it doesn’t.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Thank you,</div><div style="margin: 0px; line-height: normal;" class="">Sam</div></div></body></html>