<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="">Opinions inline:<div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On 18 Aug 2016, at 07:43, Sikhapol Saijit via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; 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 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=""> -&gt; </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;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" 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;" class=""> </span><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="">? </span><span style="font-variant-ligatures: no-common-ligatures" class="">couldFailButWillNot</span><span style="font-variant-ligatures: no-common-ligatures;" class="">() </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><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="">&nbsp; &nbsp; </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&nbsp;<b style="font-family: Menlo; font-size: 11px;" class="">Optional(42)</b>&nbsp;on both Swift 2 and Swift 3.</div><div class="">I always have the impression that when a variable is resolved with&nbsp;<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="">&nbsp;</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&nbsp;<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>&nbsp;has higher precedence than&nbsp;<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>&nbsp;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="">?&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Int`</span></font>&nbsp;evaluated as&nbsp;<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&nbsp;<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>&nbsp;can be used with non-method-call.</div><div class="">This code:&nbsp;<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>&nbsp;will print&nbsp;<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&nbsp;<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>&nbsp;can be used with a "non-method-call" and return an optional of the type that follows?</div></div></div></blockquote><div><br class=""></div><div>I think this is the real solution. try and try? should not be allowed on non-throwing functions or expressions.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">2. Should we design&nbsp;<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>&nbsp;to have higher precedence than&nbsp;<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="">?&nbsp;</span>or any operators at all?</div><div class="">My intuition tells me that&nbsp;</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>&nbsp;</div></div></div></blockquote><div><br class=""></div><div>That’s worth considering. try feels like it should tie very strongly with the throwing expression.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><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&nbsp;<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.&nbsp;</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></div></blockquote><div><br class=""></div><div>I don’t think this is the solution. Even if it was, how would you expect to “remove” them from Swift? Optionals are simply an enum with an associated value. We’d have to introduce a language feature to restrict values that can be stored in enum cases? It sounds awfully complicated.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><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=""> -&gt; </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;" class="">&nbsp; &nbsp; </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;" class="">&nbsp; &nbsp; </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;" class="">&nbsp; &nbsp; </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></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>