<div dir="ltr"><div>On Thu, Aug 18, 2016 at 11:30 AM, John McCall <span dir="ltr"><<a href="mailto:rjmccall@apple.com" target="_blank">rjmccall@apple.com</a>></span> wrote:<br></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><span><blockquote type="cite"><div>On Aug 18, 2016, at 8:46 AM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>> wrote:</div><div>The issue would be that, in the case of "try? foo()", nil and .some(nil) might mean very different things.<br></div></blockquote><div><br></div></span>This is true of a?.foo() as well. But yes, I think it is more likely that someone would want to treat them differently for try?.</div></div></blockquote><div><br></div></div></div></div><div style="white-space:pre-wrap">Agreed.<br><br>My proposed solution was half-baked, but it may be workable--I'm not suggesting typing decisions based on a dynamic property, of course. It'd be something like this:<br><br>`as?` would produce a result of a type named something like CastingOptional<T>, which on assignment or essentially any other operation is promoted/bridged/[insert much more correct term here] to an Optional<T> like how T is automatically promoted to Optional<T>. However, `try?` will not wrap a CastingOptional<T> into an Optional<Optional<T>>.</div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span><font color="#888888"><div>John.</div></font></span><div><div><div><br><blockquote type="cite"><div><div class="gmail_quote"><div dir="ltr">On Thu, Aug 18, 2016 at 10:40 John McCall <<a href="mailto:rjmccall@apple.com" target="_blank">rjmccall@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On Aug 18, 2016, at 8:19 AM, Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div dir="ltr">Lots of interesting points here. I do think there's an improvement possible here, but it's actually along the lines of Sam'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'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's explanation. He's not saying "try?" attaches with lower or higher precedence as compared to "as?". Rather, I think the mental model is that "try?" 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't, "try?" 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'm in favor of warning instead of error, for precisely the internal discussion rationale communicated by Slava. I'm willing to live with "try? 42" being only a warning if that means my code won't stop compiling when someone decides a library function doesn'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 "try?" does prefix at least one throwing function, so you wouldn't get an error anyway.</div><div><br></div><div>C. However, given the thinking in (A), I do think how "try?" composes with "as?" is a little counterintuitive or at least overly ceremonious, though technically it is possible to reason through.</div><div><br></div><div>It'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 "try?" after all means "give me nil if anything throws," it kind of makes less sense that you get all this nesting and detailed information when it composes with "as?". If you really wanted that level of detail, you could always evaluate "try?" and "as?" in separate statements. What I'd propose instead is this:</div><div><br></div><div>If "try?" is composed with "as?", and "as?" yields "nil", then "try?" should not wrap that value in another optional.</div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div>We can't make the typing decision dependent on a dynamic property like whether the cast fails. And I don't like the idea of changing its typing rule based on the form of the nested expression. But we could make "try? foo()" avoid adding an extra level of optionality, the same way that "a?.foo()" does.</div></div><div style="word-wrap:break-word"><div><br></div><div>John.</div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><div><div dir="ltr"><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"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></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><blockquote type="cite"><div>On Aug 18, 2016, at 3:42 PM, Slava Pestov <<a href="mailto:spestov@apple.com" target="_blank">spestov@apple.com</a>> 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 <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> 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 <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> 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)">throws</span><span><span> </span>-><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)">as</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> 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 "non-method-call" 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 'try' 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 'try' 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><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> </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.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(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>-><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>_______________________________________________<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/mailman/listinfo/swift-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">_______________________________________________</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/mailman/listinfo/swift-evolution</a></div></blockquote></div></blockquote></div></div></div><br></div><br>_______________________________________________<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div></div></div></div>
_______________________________________________<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/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></blockquote></div>
</div></blockquote></div><br></div></div></div></blockquote></div></div></div>