<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="">Hi,<div class=""><br class=""></div><div class="">Two points:&nbsp;</div><div class=""><br class=""></div><div class="">1) What you want to do is a common operation in functional programming called flatMap. Optional type already supports it. To get&nbsp;“x != nil ? f(x) : nil” you say:&nbsp;</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class=""><span style="color: #4f8187" class="">x</span><span style="color: #000000" class="">.</span>flatMap<span style="color: #000000" class="">(</span><span style="color: #31595d" class="">f</span><span style="color: #000000" class="">)</span></div></div><div class=""><br class=""></div><div class="">2) You don’t need multiple versions, because there is a subtype-supertype relationship between functions: You need to specify the most general signature in your single function. The most general form of the function is:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class="">(</span>XType<span style="color: #000000" class="">) </span><span style="color: #ba2da2" class="">throws</span><span style="color: #000000" class=""> -&gt; </span>RetType<span style="color: #000000" class="">?</span></div></div><div class=""><br class=""></div><div class="">To handle throwing case, you will need to define the return type of your own function as:</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="">fnil&lt;...&gt; (...) <span style="color: #ba2da2" class="">rethrows</span> -&gt; <span style="color: #4f8187" class="">RetType</span>?</div></div><div class=""><br class=""><div>Then it will accept any of the following signatures and it just works:</div><div><br class=""></div><div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> f0(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #4f8187" class="">XType</span>) -&gt; <span style="color: #4f8187" class="">RetType</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> f1(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #4f8187" class="">XType</span>) -&gt; <span style="color: #4f8187" class="">RetType</span>?</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> f2(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #4f8187" class="">XType</span>?) -&gt; <span style="color: #4f8187" class="">RetType</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> f3(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #4f8187" class="">XType</span>?) -&gt; <span style="color: #4f8187" class="">RetType</span>?</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> f0t(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #4f8187" class="">XType</span>) <span style="color: #ba2da2" class="">throws</span> -&gt; <span style="color: #4f8187" class="">RetType</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> f1t(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #4f8187" class="">XType</span>) <span style="color: #ba2da2" class="">throws</span> -&gt; <span style="color: #4f8187" class="">RetType</span>?</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> f2t(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #4f8187" class="">XType</span>?) <span style="color: #ba2da2" class="">throws</span> -&gt; <span style="color: #4f8187" class="">RetType</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> f3t(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #4f8187" class="">XType</span>?) <span style="color: #ba2da2" class="">throws</span> -&gt; <span style="color: #4f8187" class="">RetType</span>?</div></div><div><br class=""></div><div>Hope this helps,</div><div>Hooman</div><div><br class=""><blockquote type="cite" class=""><div class="">On Jan 11, 2018, at 5:55 PM, Kenny Leung via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@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; -webkit-line-break: after-white-space;" class="">Hi All.<div class=""><br class=""></div><div class="">I’m trying to write a utility method that is kind of the opposite of “x ?? y”, which means “x != nil ? &nbsp;x : y”. I want “x != nil ? f(x) : nil” This is not difficult to write, but if you have "f(x)-&gt;z" and "g(x)-&gt;z?", I seem to need two versions of the function in order to handle it. Is there any way to do this with only one function?</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">func</span> fnil&lt;XType,RetType&gt; (</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">_</span> x:<span style="color: #4f8187" class="">XType</span>?,</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color: #ba2da2" class="">_</span><span style="" class=""> f:(</span>XType<span style="" class="">)-&gt;</span>RetType</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; )</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; -&gt; <span style="color: #4f8187" class="">RetType</span>?</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">if</span> <span style="color: #ba2da2" class="">let</span> x = x {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> f(x)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; } <span style="color: #ba2da2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(186, 45, 162);" class="">public</span> <span style="color: rgb(186, 45, 162);" class="">func</span> fnil&lt;XType,RetType&gt; (</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">_</span> x:<span style="color: #4f8187" class="">XType</span>?,</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color: #ba2da2" class="">_</span><span style="" class=""> g:(</span>XType<span style="" class="">)-&gt;</span>RetType<span style="" class="">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; )</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; -&gt; <span style="color: #4f8187" class="">RetType</span>?</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">if</span> <span style="color: #ba2da2" class="">let</span> x = x {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> g(x)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; } <span style="color: #ba2da2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp;&nbsp;<span style="color: #ba2da2" class="">private</span> <span style="color: #ba2da2" class="">func</span> f(<span style="color: #ba2da2" class="">_</span> x:<span style="color: #703daa" class="">Int</span>) -&gt; <span style="color: #703daa" class="">Int</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> x</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">private</span> <span style="color: #ba2da2" class="">func</span> g(<span style="color: #ba2da2" class="">_</span> x:<span style="color: #703daa" class="">Int</span>) -&gt; <span style="color: #703daa" class="">Int</span>? {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">if</span> x == <span style="color: #272ad8" class="">5</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; } <span style="color: #ba2da2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> x</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">func</span> testFnil() {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3e1e81" class="">XCTAssertNil</span>(<span style="color: #31595d" class="">fnil</span>(<span style="color: #ba2da2" class="">nil</span>, {<span style="color: #31595d" class="">f</span>($0)}))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3e1e81" class="">XCTAssertEqual</span>(<span style="color: #272ad8" class="">7</span>, <span style="color: #31595d" class="">fnil</span>(<span style="color: #272ad8" class="">7</span>,{<span style="color: #31595d" class="">f</span>($0)}))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3e1e81" class="">XCTAssertNil</span>(<span style="color: #31595d" class="">fnil</span>(<span style="color: #ba2da2" class="">nil</span>, {<span style="color: #31595d" class="">g</span>($0)}))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3e1e81" class="">XCTAssertEqual</span>(<span style="color: #272ad8" class="">7</span>, <span style="color: #31595d" class="">fnil</span>(<span style="color: #272ad8" class="">7</span>, {<span style="color: #31595d" class="">g</span>($0)}))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3e1e81" class="">XCTAssertNil</span>(<span style="color: #31595d" class="">fnil</span>(<span style="color: #272ad8" class="">5</span>, {<span style="color: #31595d" class="">g</span>($0)}))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div></div><div class=""><br class=""></div><div class="">Thanks!</div><div class=""><br class=""></div><div class="">-Kenny</div><div class=""><br class=""></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>