This question was asked a few months back on this list--I can&#39;t recall exactly when, but there were some useful comments there that would be worth revisiting.<br><div class="gmail_quote"><div dir="ltr">On Fri, Dec 2, 2016 at 16:16 Tim Vermeulen via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; 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" class="gmail_msg"><div dir="auto" style="word-wrap:break-word" class="gmail_msg">Given the function<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><div style="margin:0px;line-height:normal" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">func</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> mightReturnInt() </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">throws</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">Any</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></div><div style="margin:0px;line-height:normal;color:rgb(186,45,162)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">3</span></div><div style="margin:0px;line-height:normal" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div><div style="margin:0px;line-height:normal" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><br class="gmail_msg"></span></div></div><div class="gmail_msg">combining `<span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">try</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="gmail_msg">?</span>` and `<span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">as</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="gmail_msg">?</span>` has a somewhat unexpected result:</div></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">if</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> int = </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">try</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? </span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="gmail_msg">mightReturnInt</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">() </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">as</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="gmail_msg">print</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg">(int) </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">// =&gt; Optional(3)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">You’d need to add parentheses to end up with a non-optional:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><div style="margin:0px;line-height:normal" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">if</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> int = (</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">try</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? </span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="gmail_msg">mightReturnInt</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">()) </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">as</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></div><div style="margin:0px;line-height:normal;color:rgb(0,132,0)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="gmail_msg">print</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg">(int) </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">// =&gt; 3</span></div><div style="margin:0px;line-height:normal" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div></div></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Apparently, `<span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">try</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="gmail_msg">?</span>`’s precedence is lower than `<span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">as</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="gmail_msg">?</span>`’s precedence. Should this be changed?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">On a similar note:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">func</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> mightReturnInt() </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">throws</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">3</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"></span><br class="gmail_msg"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">if</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> int = </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">try</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? </span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="gmail_msg">mightReturnInt</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">() {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="gmail_msg">print</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000" class="gmail_msg">(int) </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">// =&gt; Optional(3)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Here, `<span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">try</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="gmail_msg">?</span>` acts as a map, not as a flatMap, resulting in a double optional (and in a single optional after unwrapping it). None of the possible workarounds are particularly pretty:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">if</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> temp = </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">try</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? </span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="gmail_msg">mightReturnInt</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(), let int = temp {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="gmail_msg">print</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(int) </span><span style="font-variant-ligatures:no-common-ligatures;color:#008400" class="gmail_msg">// =&gt; 3</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">or:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">if</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">let</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> int = (</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">try</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? </span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="gmail_msg">mightReturnInt</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">()) ?? </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">nil</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="gmail_msg">print</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(int) </span><span style="font-variant-ligatures:no-common-ligatures;color:#008400" class="gmail_msg">// =&gt; 3</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Once again, is this behaviour expected and desired, or should it be changed while it still can?</div></div></div>_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>