<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">This is all still in the brainstorming realm. The issue is that doSomething looks like it should always be called. I think that the nil coalescing operator works here:<div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">func</span> doSomething(value: <span style="color: rgb(112, 61, 170);" class="">Int</span>) -> <span style="color: rgb(112, 61, 170);" class="">Int</span> {</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(187, 44, 162);" class="">return</span> value</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">let</span> pf : <span style="color: rgb(112, 61, 170);" class="">Int</span>? = <span style="color: rgb(39, 42, 216);" class="">5</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">let</span> py : <span style="color: rgb(112, 61, 170);" class="">Int</span>? = <font color="#272ad8" class="">nil</font></div><div class=""><span style="color: rgb(39, 42, 216);" class=""><br class=""></span></div><div class=""><span style="color: rgb(39, 42, 216);" class="">// the following is:</span></div><div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><span style="color: rgb(187, 44, 162);" class="">let</span><span style="color: rgb(0, 0, 0);" class=""> gf = pf ??</span><span style="color: rgb(0, 0, 0);" class=""> </span>doSomething<span style="color: rgb(0, 0, 0);" class="">(</span><span style="color: rgb(79, 129, 135);" class="">pf!</span><span style="color: rgb(0, 0, 0);" class="">) </span></div><div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">let</span><span style="color: rgb(0, 0, 0);" class=""> gy = py ?? </span>doSomething<span style="color: rgb(0, 0, 0);" class="">(</span><span style="color: rgb(79, 129, 135);" class="">py!</span><span style="color: rgb(0, 0, 0);" class="">)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(0, 0, 0);" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">gf -> 5</div></div><div style="color: rgb(0, 0, 0); font-family: Helvetica;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">gy -> nil</div></div></div><div class=""><div style="color: rgb(0, 0, 0);" class=""><span style="color: rgb(39, 42, 216);" class=""><br class=""></span></div><div style="color: rgb(0, 0, 0);" class=""><span style="color: rgb(39, 42, 216);" class="">// is the equivalent of:</span></div></div></div><div class=""><span style="color: rgb(187, 44, 162);" class="">let</span> ff = <span style="color: rgb(79, 129, 135);" class="">pf</span>.<span style="color: rgb(61, 29, 129);" class="">map</span> { <span style="color: rgb(49, 89, 93);" class="">doSomething</span>($0) }</div></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">let</span> fy = <span style="color: rgb(79, 129, 135);" class="">py</span>.<span style="color: rgb(61, 29, 129);" class="">map</span> { <span style="color: rgb(49, 89, 93);" class="">doSomething</span>($0) }</div><div class=""><br class=""></div></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">ff -> 5</div></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">fy -> nil</div></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica;" class="">This works with the language as it stands today but I don’t like the forced unwrap. However, with the rest of the proposal this would work because pf? would give you the unwrapped value, which would not be nil at that point.</div><div style="color: rgb(0, 0, 0); font-family: Helvetica;" class=""><br class=""></div><div style="color: rgb(0, 0, 0); font-family: Helvetica;" class=""><span style="font-family: Menlo; color: rgb(187, 44, 162);" class="">let</span><span style="font-family: Menlo;" class=""> gf = pf ??</span><span style="font-family: Menlo;" class=""> </span><span style="color: rgb(49, 89, 93); font-family: Menlo;" class="">doSomething</span><span style="font-family: Menlo;" class="">(</span><span style="font-family: Menlo; color: rgb(79, 129, 135);" class="">pf?</span><span style="font-family: Menlo;" class="">) </span></div><div class=""><div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">let</span><span style="color: rgb(0, 0, 0);" class=""> gy = py ?? </span>doSomething<span style="color: rgb(0, 0, 0);" class="">(</span><span style="color: rgb(79, 129, 135);" class="">py?</span><span style="color: rgb(0, 0, 0);" class="">)</span></div><div class=""><span style="color: rgb(0, 0, 0);" class=""><br class=""></span></div></div></div></div></div><div style="color: rgb(209, 47, 27); font-family: Menlo; margin: 0px; line-height: normal;" class=""></div><div><blockquote type="cite" class=""></blockquote></div><div>But one outstanding issue is that gf and gy would not be optional unless you explicitly write out the type like this.</div><div><br class=""></div><div><div class=""><span style="font-family: Menlo; color: rgb(187, 44, 162);" class="">let</span><span style="font-family: Menlo;" class=""> gf </span><span style="font-family: Menlo;" class="">: </span><span style="font-family: Menlo; color: rgb(112, 61, 170);" class="">Int</span><span style="font-family: Menlo;" class="">?</span><span style="font-family: Menlo;" class=""> = pf ??</span><span style="font-family: Menlo;" class=""> </span><span style="color: rgb(49, 89, 93); font-family: Menlo;" class="">doSomething</span><span style="font-family: Menlo;" class="">(</span><span style="font-family: Menlo; color: rgb(79, 129, 135);" class="">pf?</span><span style="font-family: Menlo;" class="">) </span></div><div style="color: rgb(79, 129, 135); font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">let</span><span style="color: rgb(0, 0, 0);" class=""> gy </span><span style="color: rgb(0, 0, 0);" class="">: </span><span style="color: rgb(112, 61, 170);" class="">Int</span><span style="color: rgb(0, 0, 0);" class="">? </span><span style="color: rgb(0, 0, 0);" class="">= py ?? </span>doSomething<span style="color: rgb(0, 0, 0);" class="">(</span><span style="color: rgb(79, 129, 135);" class="">py?</span><span style="color: rgb(0, 0, 0);" class="">)</span></div></div></div></div><div><br class=""></div><div>I think this is in line with the language as it is today. Unless we want to add a new sugar to upgrade an optional </div><div><br class=""></div><div><div><div class=""><span style="font-family: Menlo; color: rgb(187, 44, 162);" class="">let</span><span style="font-family: Menlo;" class=""> gf</span><span style="font-family: Menlo;" class="">?</span><span style="font-family: Menlo;" class=""> = pf ??</span><span style="font-family: Menlo;" class=""> </span><span style="color: rgb(49, 89, 93); font-family: Menlo;" class="">doSomething</span><span style="font-family: Menlo;" class="">(</span><span style="font-family: Menlo; color: rgb(79, 129, 135);" class="">pf?</span><span style="font-family: Menlo;" class="">) </span></div><div style="color: rgb(79, 129, 135); font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">let</span><span style="color: rgb(0, 0, 0);" class=""> gy</span><span style="color: rgb(0, 0, 0);" class="">? </span><span style="color: rgb(0, 0, 0);" class="">= py ?? </span>doSomething<span style="color: rgb(0, 0, 0);" class="">(</span><span style="color: rgb(79, 129, 135);" class="">py?</span><span style="color: rgb(0, 0, 0);" class="">)</span></div><div class=""><span style="color: rgb(0, 0, 0);" class=""><br class=""></span></div></div></div></div></div><div>where if there is a let or var assignment with a question mark the type is upgraded to an optional.</div><div><br class=""></div><div>------</div><div><br class=""></div><div>Moving back to the more complex example. The more complex example would require another construct. We would need to have a way to add a block.for nil coalescing but pretty sure that would not be approved and it probably creates other problems. Assuming that doSomething else is a void function. </div><div><br class=""></div><div><div class=""> let dd = n ?? { manager.doSomething(data: data, count: n!); doSomethingElse( n! ) }</div><div class=""><br class=""></div><div class="">So without adding more constructs you would have to do this for the more complex example:</div><div class=""><br class=""></div><div class=""> let dd = n ?? manager.doSomething(data: data, count: n?)</div><div class=""> if let n = n {</div><div class=""> <span class="Apple-tab-span" style="white-space:pre">        </span>doSomethingElse( n ) </div><div class=""> }</div><div class=""><br class=""></div></div><div>In which case you might as well do this:</div></div><div><br class=""></div><div> let dd : Int? = nil</div><div><div><div class=""> if let n = n {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span> dd = manager.doSomething(data: data, count: n)</div><div class=""> <span class="Apple-tab-span" style="white-space: pre;">        </span> doSomethingElse( n ) </div><div class=""> }</div><div class=""><br class=""></div><div class="">But I think the more complex example will be rare and the last example is fine should you have a complex situation</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div><div class=""><blockquote type="cite" class="">Another conundrum: are the optional-pattern arguments evaluated before any other arguments? After?</blockquote><br class=""></div><div class=""><blockquote type="cite" class="">I think the two statements would behave separately (as if you had used map twice).<br class=""><br class="">If you want them to act together, you could do this: <br class=""><br class="">{ manager.doSomething(data: data, count: $0); doSomethingElse($0) }(n?)<br class=""><br class="">Another aspect worth considering is whether this syntax should work with multiple optionals used in the same expression. (e.g. if they are all non-nil, the expression is evaluated.)</blockquote></div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>