<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">I think the best way is </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">do</span><span style=""> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> a = </span><span style="color:rgb(187,44,162)">try</span><span style="color:rgb(0,0,0)"> </span><span style="">couldFailButWillNot</span><span style="color:rgb(0,0,0)">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">    </span><span style="color:rgb(187,44,162)">if</span><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> b = a </span><span style="color:rgb(187,44,162)">as</span><span style="">? </span><span style="color:rgb(112,61,170)">Int</span><span style=""> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">        </span><span style="color:rgb(61,29,129)">print</span><span style="">(b)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">    }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">} </span><span style="color:rgb(187,44,162)">catch</span><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> error {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">    </span><span style="color:rgb(61,29,129)">print</span><span style="">(error)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">}</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">It is longer, but much clearer.  </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Below code will work, but not as clear. You don&#39;t know whether it is a or b causes the failure. It is not a good practice to use try? if that is not on purpose.</div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">if</span><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> a = </span><span style="color:rgb(187,44,162)">try</span><span style="">? </span><span style="color:rgb(49,89,93)">couldFailButWillNot</span><span style="">(), </span><span style="color:rgb(187,44,162)">let</span><span style=""> b = a </span><span style="color:rgb(187,44,162)">as</span><span style="">? </span><span style="color:rgb(112,61,170)">Int</span><span style=""> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">    </span><span style="color:rgb(61,29,129)">print</span><span style="">(b)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">}</span></p></div><div class="gmail_default" style="font-family:georgia,serif">or</div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(187,44,162)">if</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> a = (</span><span style="color:rgb(187,44,162)">try</span><span style="color:rgb(0,0,0)">? </span><span style="">couldFailButWillNot</span><span style="color:rgb(0,0,0)">()) </span><span style="color:rgb(187,44,162)">as</span><span style="color:rgb(0,0,0)">? </span><span style="color:rgb(112,61,170)">Int</span><span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">    </span><span style="color:rgb(61,29,129)">print</span><span style="">(a)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">}</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 21, 2016 at 4:25 PM, Sikhapol Saijit via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi Nick,<div><br></div><div>The code was simplified for demonstration.</div><div>You’re right that in a simple case like that I wouldn’t need to use <font face="Courier">`as?`</font>.</div><div><br></div><div>In my actual case it was something like this:</div><div><br></div><div><font face="Courier">func parse(JSON: Data) throws -&gt; Any {</font></div><div><font face="Courier">    // …</font></div><div><font face="Courier">}</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">if let dict = try? parse(JSON: json) as? [String: Any] {</font></div><div><font face="Courier">    // assume dict is a valid [String: Any] dictionary</font></div><div><font face="Courier">    // …</font></div><div><font face="Courier">}</font></div><div><font face="Courier"><br></font></div><div>Sure, this could be written as:</div><div><br></div><div><font face="Courier">if let jsonObject = try? parse(JSON: json), </font></div><div><font face="Courier">let dict = jsonObject as? [String: Any] {</font></div><div><font face="Courier">    // …</font></div><div><font face="Courier">}</font></div><div><br></div><div>But I just wonder why the first case behave as it does and whether it should.</div><div><br></div><div>Sam</div><div><div class="h5"><div><br><div><blockquote type="cite"><div>On Jul 21, 2016, at 3:03 PM, Nicholas Outram &lt;<a href="mailto:nicholas.outram@icloud.com" target="_blank">nicholas.outram@icloud.com</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word"><div>Are we saying this function </div><div><br></div><div><div style="font-family:HelveticaNeue-Thin"><font face="Courier">func couldFailButWillNot() throws -&gt; Any {</font></div><div style="font-family:HelveticaNeue-Thin"><font face="Courier">  return 42</font></div><div style="font-family:HelveticaNeue-Thin"><font face="Courier">}</font></div><div style="font-family:HelveticaNeue-Thin"><font face="Courier"><br></font></div><div style="font-family:HelveticaNeue-Thin"><font face="Courier">has an implicit Int return type?</font></div><div style="font-family:HelveticaNeue-Thin"><font face="Courier"><br></font></div><div style="font-family:HelveticaNeue-Thin"><font face="Courier">If so, there is actually no need for as? (which will result in an Optional - which I thought that was the behaviour in Swift 2 as well?)</font></div><div style="font-family:HelveticaNeue-Thin"><font face="Courier"><br></font></div><div style="font-family:HelveticaNeue-Thin"><br></div><div style="font-family:HelveticaNeue-Thin"><br></div><div><blockquote type="cite"><div>On 21 Jul 2016, at 08:59, Nicholas Outram via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word"><div style="margin:0px;line-height:normal;color:rgb(52,149,175)"><span style="color:rgb(4,51,255)"><font face="HelveticaNeue-Light" size="4">The issue is related to the as? </font></span></div><div style="margin:0px;line-height:normal;color:rgb(52,149,175)"><span style="color:rgb(4,51,255)"><font face="HelveticaNeue-Light" size="4"><br></font></span></div><div style="margin:0px;line-height:normal"><span style="color:rgb(4,51,255)"><font face="HelveticaNeue-Light" size="4">Try </font></span><font color="#0433ff" face="HelveticaNeue-Light" size="4">this:</font></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(52,149,175)"><span style="color:#0433ff"><br></span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(52,149,175)"><span style="color:#0433ff">if</span><span> </span><span style="color:#0433ff">let</span><span> a = </span><span style="color:#0433ff">try</span><span>? </span><span>couldFailButWillNot</span><span>()  {</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>   </span><span style="color:#3495af">print</span><span>(a)</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span><br></span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span><br></span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>Nick</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span><br></span></div><div><blockquote type="cite"><div>On 21 Jul 2016, at 07:13, Sikhapol Saijit via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word">Hi Swift Community,<div><br></div><div><br></div><div>Yesterday I tried this code:</div><div><br></div><div><font face="Courier">```</font></div><div><div><font face="Courier">func couldFailButWillNot() throws -&gt; Any {</font></div><div><font face="Courier">  return 42</font></div><div><font face="Courier">}</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">if let a = try? couldFailButWillNot() as? Int {</font></div><div><font face="Courier">  print(a)</font></div><div><font face="Courier">}</font></div></div><div><font face="Courier">```</font></div><div><br></div><div>And was surprised that the output was <i>&quot;Optional(42)”</i> on both Swift 2.2 and Swift 3.</div><div>I always have the impression that when a variable is resolved with <font face="Courier">`if let`</font> it will never be optional.</div><div><br></div><div>So, with a little investigation, I found out that it happens because <font face="Courier">`as?`</font> has higher precedence than <font face="Courier">`try?`</font> and is evaluated first.</div><div>And the whole expression <font face="Courier">`try? couldFailButWillNot() as? Int`</font> evaluated as <i>“Optional(Optional(42))”</i>.</div><div><br></div><div>Also, I’m surprised that <font face="Courier">`try?`</font> can be used with non-method-call.</div><div>This code: <font face="Courier">`print(try? 42)`</font> will print <i>“Optional(42)”</i>.</div><div><br></div><div>So, the questions are:</div><div><br></div><div>1. Is it intentional that <font face="Courier">`try?`</font> can be used with non-method-call and return an optional of the type that follows?</div><div><br></div><div>2. Should we design <font face="Courier">`try?`</font> to have higher precedence than <font face="Courier">`as?`</font>. </div><div>My intuition tells me that </div><div><font face="Courier">`let a = try? couldFailButWillNot() as? Int`</font></div><div><font face="Courier">and</font> </div><div><font face="Courier">`</font><span style="font-family:Courier">let a = (try? couldFailButWillNot()) as? Int</span><font face="Courier">`</font> </div><div>should be equivalent.</div><div><br></div><div>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" target="_blank">https://developer.apple.com/swift/blog/?id=12</a>))</div><div>For me “Optional(nil)” (aka “Optional.Some(Optional.None))”) doesn’t make any sense at all. </div><div>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><br></div><div><br><div>Thank you</div><div>Sikhapol Saijit (Sam)</div><div>iOS Developer, Taskworld, Bangkok</div></div></div>_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div>_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></div></blockquote></div><br></div></div></div></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>