<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=""><div class="">Yep, it looks like Optional.flatmap() is what I need. Thanks!</div><div class=""><br class=""></div><div class="">-Kenny</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 11, 2018, at 6:55 PM, Hooman Mehr <<a href="mailto:hooman@mac.com" class="">hooman@mac.com</a>> 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="">Hi,<div class=""><br class=""></div><div class="">Two points: </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 “x != nil ? f(x) : nil” you say: </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="" class="">.</span>flatMap<span style="" class="">(</span><span style="color: #31595d" class="">f</span><span style="" 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="" class="">(</span>XType<span style="" class="">) </span><span style="color: #ba2da2" class="">throws</span><span style="" class=""> -> </span>RetType<span style="" 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<...> (...) <span style="color: #ba2da2" class="">rethrows</span> -> <span style="color: #4f8187" class="">RetType</span>?</div></div><div class=""><br class=""><div class="">Then it will accept any of the following signatures and it just works:</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=""><span style="color: #ba2da2" class="">func</span> f0(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #4f8187" class="">XType</span>) -> <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>) -> <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>?) -> <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>?) -> <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> -> <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> -> <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> -> <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> -> <span style="color: #4f8187" class="">RetType</span>?</div></div><div class=""><br class=""></div><div class="">Hope this helps,</div><div class="">Hooman</div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jan 11, 2018, at 5:55 PM, Kenny Leung via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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 ? x : y”. I want “x != nil ? f(x) : nil” This is not difficult to write, but if you have "f(x)->z" and "g(x)->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<XType,RetType> (</div><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="">_</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=""> </span><span style="color: #ba2da2" class="">_</span><span style="" class=""> f:(</span>XType<span style="" class="">)-></span>RetType</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=""> -> <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=""> <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=""> <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=""> } <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=""> <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=""> }</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<XType,RetType> (</div><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="">_</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=""> </span><span style="color: #ba2da2" class="">_</span><span style="" class=""> g:(</span>XType<span style="" class="">)-></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=""> )</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> -> <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=""> <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=""> <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=""> } <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=""> <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=""> }</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=""> <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>) -> <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=""> <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=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <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=""> <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>) -> <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=""> <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=""> <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=""> } <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=""> <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=""> }</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); min-height: 13px;" class=""> <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=""> <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=""> <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=""> <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=""> <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=""> <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=""> <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=""> <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=""> }</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=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></body></html>