<div dir="ltr">Lots of interesting points here. I do think there&#39;s an improvement possible here, but it&#39;s actually along the lines of Sam&#39;s original suggestion #3 (not vis-a-vis all of Swift, but specifically for how try? composes with as?):<div><br></div><div>A. I&#39;m in favor of the current behavior where try prefixes an entire statement: it solves the precise issue of multiple nested optionals or multiple unwrapping of optionals in the situation where one statement has calls to many throwing functions. It says instead, I want nil if anything in this statement throws, otherwise, give me .some(value).</div><div><br></div><div>Sam--I think you may have misunderstood Charles&#39;s explanation. He&#39;s not saying &quot;try?&quot; attaches with lower or higher precedence as compared to &quot;as?&quot;. Rather, I think the mental model is that &quot;try?&quot; prefixes the whole right-hand side (rhs), and if *any* call on the rhs throws, the whole rhs evaluates to nil, but if *any* call could potentially throw but doesn&#39;t, &quot;try?&quot; wraps the entire rhs and gives you .some(value). IMO, this is pretty sensible for the reason he gives.</div><div><br></div><div>B. I&#39;m in favor of warning instead of error, for precisely the internal discussion rationale communicated by Slava. I&#39;m willing to live with &quot;try? 42&quot; being only a warning if that means my code won&#39;t stop compiling when someone decides a library function doesn&#39;t need to throw.</div><div><br></div><div>Sam--here, changing warning to error would not solve your original problem, because in that example &quot;try?&quot; does prefix at least one throwing function, so you wouldn&#39;t get an error anyway.</div><div><br></div><div>C. However, given the thinking in (A), I do think how &quot;try?&quot; composes with &quot;as?&quot; is a little counterintuitive or at least overly ceremonious, though technically it is possible to reason through.</div><div><br></div><div>It&#39;s true that currently you can use the multiple nested optionals to figure out whether either a throwing function threw (but not which throwing function out of potentially more than one) or whether the cast did not succeed. But, since &quot;try?&quot; after all means &quot;give me nil if anything throws,&quot; it kind of makes less sense that you get all this nesting and detailed information when it composes with &quot;as?&quot;. If you really wanted that level of detail, you could always evaluate &quot;try?&quot; and &quot;as?&quot; in separate statements. What I&#39;d propose instead is this:</div><div><br></div><div>If &quot;try?&quot; is composed with &quot;as?&quot;, and &quot;as?&quot; yields &quot;nil&quot;, then &quot;try?&quot; should not wrap that value in another optional.</div><div><br></div><div>Does that sound sensible?</div><div><div><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Aug 18, 2016 at 3:54 AM, Sikhapol Saijit via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@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"><br><div><div><div class="h5"><blockquote type="cite"><div>On Aug 18, 2016, at 3:42 PM, Slava Pestov &lt;<a href="mailto:spestov@apple.com" target="_blank">spestov@apple.com</a>&gt; wrote:</div><br><div><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><br>On Aug 18, 2016, at 12:52 AM, David Hart via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">Opinions inline:</span><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><blockquote type="cite"><div>On 18 Aug 2016, at 07:43, Sikhapol Saijit via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word">Hi all,<div><br></div><div><br></div><div>Yesterday I tried this code:</div><div><br></div><div><div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(186,45,162)">func</span><span><span> </span>couldFailButWillNot()<span> </span></span><span style="color:rgb(186,45,162)">thr<wbr>ows</span><span><span> </span>-&gt;<span> </span></span><span style="color:rgb(186,45,162)">Any</span><span><span> </span>{</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)"><span>   <span> </span></span><span>return</span><span><span> </span></span><span style="color:rgb(39,42,216)">42</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(186,45,162)">if</span><span><span> </span></span><span style="color:rgb(186,45,162)">let</span><span><span> </span>a =<span> </span></span><span style="color:rgb(186,45,162)">try</span><span>?<span> </span></span><span>couldFailButWillNot</span><span>()<span> </span></span><span style="color:rgb(186,45,162)">a<wbr>s</span><span>?<span> </span></span><span style="color:rgb(112,61,170)">Int</span><span><span> </span>{</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>   <span> </span></span><span style="color:rgb(62,30,129)">print</span><span>(a)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div><span><br></span></div></div></div></div></div><div>And was surprised that the output was <b style="font-family:Menlo;font-size:11px">Optional(42)</b> on both Swift 2 and Swift 3.</div><div>I always have the impression that when a variable is resolved with <font><span style="font-family:Menlo;color:rgb(186,45,162);font-size:11px">if</span><span style="font-family:Menlo;font-size:11px"><span> </span></span><span style="font-family:Menlo;color:rgb(186,45,162);font-size:11px">let</span><span style="color:rgb(186,45,162)"> </span>it will never be optional.</font></div><div><br></div><div>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">as</span><span style="font-family:Menlo;font-size:11px">?</span> has higher precedence than <span style="color:rgb(186,45,162);font-family:Menlo;font-size:11px">try</span><span style="font-family:Menlo;font-size:11px">?</span> and is evaluated first.</div><div>And the whole expression<span> </span><font face="Menlo" style="font-size:11px">`<span style="color:rgb(186,45,162)">try</span><span>? couldFailButWillNot()<span> </span></span><span style="color:rgb(186,45,162)">as</span><span>? </span><span style="color:rgb(112,61,170)">Int`</span></font><wbr> evaluated as <span style="font-size:11px"><font face="Menlo"><b>Optional(Optional(42))</b></font></span>.</div><div><br></div><div>Also, I’m surprised that <span style="font-family:Menlo;font-size:11px;color:rgb(186,45,162)">try</span><span style="font-family:Menlo;font-size:11px">?</span> can be used with non-method-call.</div><div>This code: <span style="font-family:Menlo;font-size:11px">`print(</span><span style="font-family:Menlo;font-size:11px;color:rgb(186,45,162)">try</span><span style="font-family:Menlo;font-size:11px">?<span> </span></span><span style="font-family:Menlo;font-size:11px;color:rgb(39,42,216)">42</span><span style="font-family:Menlo;font-size:11px">)`</span> will print <b style="font-family:Menlo;font-size:11px">Optional(42)</b>.</div><div><br></div><div>So, the questions are:</div><div><br></div><div>1. Is it intentional that <span style="font-family:Menlo;font-size:11px;color:rgb(186,45,162)">try</span><span style="font-family:Menlo;font-size:11px">?</span> can be used with a &quot;non-method-call&quot; and return an optional of the type that follows?</div></div></div></blockquote><div><br></div><div>I think this is the real solution. try and try? should not be allowed on non-throwing functions or expressions.</div></div></div></div></blockquote><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">This is a warning right now — do you think it should be an error?</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span>Slavas-MacBook-Pro:~ slava$ cat ttt.swift </span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span>func f() {}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span>func g() {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span>  try f()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span>  try? f()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span>Slavas-MacBook-Pro:~ slava$ swiftc ttt.swift </span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span><b>ttt.swift:4:3:<span> </span></b></span><span style="color:rgb(213,59,211)"><b>warning:<span> </span></b></span><span><b>no calls to throwing functions occur within &#39;try&#39; expression</b></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span>  try f()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,188,38);background-color:rgb(255,255,255)"><span><b>  ^</b></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span><b>ttt.swift:5:8:<span> </span></b></span><span style="color:rgb(213,59,211)"><b>warning:<span> </span></b></span><span><b>no calls to throwing functions occur within &#39;try&#39; expression</b></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span>  try? f()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,188,38);background-color:rgb(255,255,255)"><span><b>       ^</b></span></div></div></div></blockquote><div><br></div></div></div><div><div>Thank you Slava,</div><div><br></div><div>While I think using try/try? on anything but a throwing function call should be an error, right now it even works with anything. `try? 42` will just wrap 42 in an optional and give some warning now.</div></div><div><div class="h5"><br><blockquote type="cite"><div><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div>2. Should we design <span style="font-family:Menlo;font-size:11px;color:rgb(186,45,162)">try</span><span style="font-family:Menlo;font-size:11px">?</span> to have higher precedence than <span style="font-family:Menlo;font-size:11px;color:rgb(186,45,162)">as</span><span style="font-family:Menlo;font-size:11px">? </span>or any operators at all?</div><div>My intuition tells me that </div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(186,45,162)">let</span><span><span> </span>a =<span> </span></span><span style="color:rgb(186,45,162)">try</span><span>? couldFailButWillNot()<span> </span></span><span style="color:rgb(186,45,162)">as</span><span>?<span> </span></span><span style="color:rgb(112,61,170)">Int</span></div></div><div>should be equivalent to</div><div><span style="font-family:Menlo;font-size:11px;color:rgb(186,45,162)">let</span><span style="font-family:Menlo;font-size:11px"><span> </span>a = (</span><span style="font-family:Menlo;font-size:11px;color:rgb(186,45,162)">try</span><span style="font-family:Menlo;font-size:11px">? couldFailButWillNot())<span> </span></span><span style="font-family:Menlo;font-size:11px;color:rgb(186,45,162)">as</span><span style="font-family:Menlo;font-size:11px">?<span> </span></span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">Int</span><wbr> </div></div></div></blockquote><div><br></div><div>That’s worth considering. try feels like it should tie very strongly with the throwing expression.</div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><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 <a href="https://developer.apple.com/swift/blog/?id=12" target="_blank">Optionals Case Study: valuesForKeys</a>).</div><div>For me<span> </span><font face="Menlo" style="font-size:11px"><b>Optional(nil)</b></font><span> </span>(aka<span> </span><font face="Menlo" style="font-size:11px"><b>Optional<wbr>.Some(Optional.None))</b></font>) doesn’t make much sense. </div><div>Maybe, one of the solution is to always have optional of optional merged into a single level optional? Like<span> </span><font face="Menlo" style="font-size:11px"><b>Optional(Optional(<wbr>Optional(42)))</b></font><span> </span>should be the merged to and evaluated as<span> </span><font face="Menlo" style="font-size:11px"><b>Optional(42)</b></font>.</div></div></div></blockquote><div><br></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><blockquote type="cite"><div><div style="word-wrap:break-word"><div>BTW, the code above is merely for a demonstration. The actual code was more of something like this:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(186,45,162)">func</span><span><span> </span>parse(JSON:<span> </span></span><span style="color:rgb(112,61,170)">Data</span><span>)<span> </span></span><span style="color:rgb(186,45,162)">throws</span><span><span> </span><wbr>-&gt;<span> </span></span><span style="color:rgb(186,45,162)">Any</span><span><span> </span>{</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>   <span> </span></span><span>// …</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(186,45,162)">if</span><span><span> </span></span><span style="color:rgb(186,45,162)">let</span><span><span> </span>dict =<span> </span></span><span style="color:rgb(186,45,162)">try</span><span>? parse(JSON: json)<span> </span></span><span style="color:rgb(186,45,162)">as</span><span>? [</span><span style="color:rgb(112,61,170)">String</span><span>:<span> </span></span><span style="color:rgb(186,45,162)">Any</span><span>] {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>   <span> </span></span><span>// 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)"><span>   <span> </span></span><span>// …</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;line-height:normal">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"><br></div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">Thank you,</div><div style="margin:0px;line-height:normal">Sam</div></div></div>______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div><br></div><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">______________________________<wbr>_________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-evolution mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-evolution@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-evolution@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a></div></blockquote></div></blockquote></div></div></div><br></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div></div></div></div></div>