<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="">I agree that this behavior is annoying. However, wouldn’t it be source-breaking to change this now?<div class=""><br class=""></div><div class="">-BJ<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jan 12, 2018, at 10:25 AM, Russ Bishop via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div 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="" class=""> w = </span><span style="color: #4f8187" class="">SomeType</span><span style="" class="">().</span>nonThrow<span style="" class="">()?.</span>nonThrow<span style="" class="">()?.</span>nonThrow<span style="" class="">()?.</span>nonThrow<span style="" 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="" class=""> x = </span><span style="color: #ba2da2" class="">try</span><span style="" class="">? </span><span style="color: #4f8187" class="">SomeType</span><span style="" class="">().</span>nonOptional<span style="" class="">().</span>nonOptional<span style="" class="">().</span>nonOptional<span style="" class="">().</span>nonOptional<span style="" 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="" class=""> y = </span><span style="color: #ba2da2" class="">try</span><span style="" class="">! </span><span style="color: #4f8187" class="">SomeType</span><span style="" class="">().</span>doThrow<span style="" class="">()?.</span>doThrow<span style="" class="">()?.</span>doThrow<span style="" class="">()?.</span>doThrow<span style="" 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="" class=""> z = </span><span style="color: #ba2da2" class="">try</span><span style="" class="">? </span><span style="color: #4f8187" class="">SomeType</span><span style="" class="">().</span>doThrow<span style="" class="">()?.</span>doThrow<span style="" class="">()?.</span>doThrow<span style="" class="">()?.</span>doThrow<span style="" 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="" 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=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>