<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Greetings swift-evolution!</div><div class=""><br class=""></div><div class="">There is currently a disconnect between optional chaining and try? when it comes to optional flattening:</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: #ba2da2" class="">struct</span> SomeType {</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">func</span> nonThrow() -&gt; <span style="color: #4f8187" class="">SomeType</span>? { <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">self</span> }</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">func</span> doThrow() <span style="color: #ba2da2" class="">throws</span> -&gt; <span style="color: #4f8187" class="">SomeType</span>? { <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">self</span> }</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">func</span> nonOptional() <span style="color: #ba2da2" class="">throws</span> -&gt; <span style="color: #4f8187" class="">SomeType</span> { <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">self</span> }</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">}</div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(49, 89, 93);" class=""><span style="color: #ba2da2" class="">let</span><span style="color: #000000" class=""> w = </span><span style="color: #4f8187" class="">SomeType</span><span style="color: #000000" class="">().</span>nonThrow<span style="color: #000000" class="">()?.</span>nonThrow<span style="color: #000000" class="">()?.</span>nonThrow<span style="color: #000000" class="">()?.</span>nonThrow<span style="color: #000000" class="">()</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(0, 132, 0);" class="">// w has type SomeType?</div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(49, 89, 93);" class=""><span style="color: #ba2da2" class="">let</span><span style="color: #000000" class=""> x = </span><span style="color: #ba2da2" class="">try</span><span style="color: #000000" class="">? </span><span style="color: #4f8187" class="">SomeType</span><span style="color: #000000" class="">().</span>nonOptional<span style="color: #000000" class="">().</span>nonOptional<span style="color: #000000" class="">().</span>nonOptional<span style="color: #000000" class="">().</span>nonOptional<span style="color: #000000" class="">()</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(0, 132, 0);" class="">// x has type SomeType?</div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(49, 89, 93);" class=""><span style="color: #ba2da2" class="">let</span><span style="color: #000000" class=""> y = </span><span style="color: #ba2da2" class="">try</span><span style="color: #000000" class="">! </span><span style="color: #4f8187" class="">SomeType</span><span style="color: #000000" class="">().</span>doThrow<span style="color: #000000" class="">()?.</span>doThrow<span style="color: #000000" class="">()?.</span>doThrow<span style="color: #000000" class="">()?.</span>doThrow<span style="color: #000000" class="">()</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(0, 132, 0);" class="">// y has type SomeType?</div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(49, 89, 93);" class=""><span style="color: #ba2da2" class="">let</span><span style="color: #000000" class=""> z = </span><span style="color: #ba2da2" class="">try</span><span style="color: #000000" class="">? </span><span style="color: #4f8187" class="">SomeType</span><span style="color: #000000" class="">().</span>doThrow<span style="color: #000000" class="">()?.</span>doThrow<span style="color: #000000" class="">()?.</span>doThrow<span style="color: #000000" class="">()?.</span>doThrow<span style="color: #000000" class="">()</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(0, 132, 0);" class="">// z has type SomeType??</div></div><div class=""><span style="color: #000000" class=""><br class=""></span></div></div></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class=""><br class=""></div><div class="">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're looking to start contributing.</div><div class=""><br class=""></div><br class=""><div class="">
Russ Bishop<br class=""> Simulator<br class=""><br class="">
</div>
<br class=""></body></html>