<div dir="ltr">This is not an optional-chaining issue <i>per se</i>. If you simply write,<div><br></div><div>







<p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-s1" style="color:rgb(186,45,162)">let</span> a = <span class="gmail-s1" style="color:rgb(186,45,162)">try</span>? <span class="gmail-s2" style="color:rgb(79,129,135)">SomeType</span>().<span class="gmail-s3" style="color:rgb(49,89,93)">doThrow</span>()</font></p>
<p class="gmail-p2" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)"><font size="1">// a has type SomeType??</font></p></div><div><br></div><div>you get a double-optional as well. Are you proposing to change that?</div><div><br></div><div>Nevin</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 12, 2018 at 12:25 PM, Russ Bishop 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;line-break:after-white-space"><div>Greetings swift-evolution!</div><div><br></div><div>There is currently a disconnect between optional chaining and try? when it comes to optional flattening:</div><div><br></div><div><br></div><div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><div style="margin:0px;font-stretch:normal;line-height:normal"><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="color:#ba2da2">struct</span> SomeType {</div><div style="margin:0px;font-stretch:normal;line-height:normal">    <span style="color:#ba2da2">func</span> nonThrow() -&gt; <span style="color:#4f8187">SomeType</span>? { <span style="color:#ba2da2">return</span> <span style="color:#ba2da2">self</span> }</div><div style="margin:0px;font-stretch:normal;line-height:normal">    <span style="color:#ba2da2">func</span> doThrow() <span style="color:#ba2da2">throws</span> -&gt; <span style="color:#4f8187">SomeType</span>? { <span style="color:#ba2da2">return</span> <span style="color:#ba2da2">self</span> }</div><div style="margin:0px;font-stretch:normal;line-height:normal">    <span style="color:#ba2da2">func</span> nonOptional() <span style="color:#ba2da2">throws</span> -&gt; <span style="color:#4f8187">SomeType</span> { <span style="color:#ba2da2">return</span> <span style="color:#ba2da2">self</span> }</div><div style="margin:0px;font-stretch:normal;line-height:normal">}</div><div style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(49,89,93)"><span style="color:#ba2da2">let</span><span style="color:#000000"> w = </span><span style="color:#4f8187">SomeType</span><span style="color:#000000">().</span>nonThrow<span style="color:#000000">()?.</span>nonThro<wbr>w<span style="color:#000000">()?.</span>nonThrow<span style="color:#000000">()?.</span>nonThrow<span style="color:#000000">()</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(0,132,0)">// w has type SomeType?</div><div style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(49,89,93)"><span style="color:#ba2da2">let</span><span style="color:#000000"> x = </span><span style="color:#ba2da2">try</span><span style="color:#000000">? </span><span style="color:#4f8187">SomeType</span><span style="color:#000000">().</span>nonOptional<span style="color:#000000">().</span>nonOp<wbr>tional<span style="color:#000000">().</span>nonOptional<span style="color:#000000">().</span>nonOpti<wbr>onal<span style="color:#000000">()</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(0,132,0)">// x has type SomeType?</div><div style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(49,89,93)"><span style="color:#ba2da2">let</span><span style="color:#000000"> y = </span><span style="color:#ba2da2">try</span><span style="color:#000000">! </span><span style="color:#4f8187">SomeType</span><span style="color:#000000">().</span>doThrow<span style="color:#000000">()?.</span>doThrow<span style="color:#000000">(<wbr>)?.</span>doThrow<span style="color:#000000">()?.</span>doThrow<span style="color:#000000">()</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(0,132,0)">// y has type SomeType?</div><div style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(49,89,93)"><span style="color:#ba2da2">let</span><span style="color:#000000"> z = </span><span style="color:#ba2da2">try</span><span style="color:#000000">? </span><span style="color:#4f8187">SomeType</span><span style="color:#000000">().</span>doThrow<span style="color:#000000">()?.</span>doThrow<span style="color:#000000">(<wbr>)?.</span>doThrow<span style="color:#000000">()?.</span>doThrow<span style="color:#000000">()</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(0,132,0)">// z has type SomeType??</div></div><div><span style="color:#000000"><br></span></div></div></div><div><br></div><div>We get a double-optional only when combining try? and optional-chaining. That is inconvenient and it would be natural to have the compiler do the flattening here.</div><div><br></div><div><br></div><div>If anyone is interested in working on the proposal or implementation please let me know. It would make a nice self-contained task if you&#39;re looking to start contributing.</div><div><br></div><br><div>
Russ Bishop<br> Simulator<br><br>
</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>