<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Looks like a bug to me.<div class=""><br class=""></div><div class="">You can work around it either by substituting the long version:</div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="font-family: Menlo; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> fn:(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = x != </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ? x! : qq</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">or by declaring qq & fn like this:</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> qq:(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">)? = { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"..."</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> fn:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = (x ?? qq)!</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">or like this:</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> qq:(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">)! = { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"..."</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> fn:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = x ?? qq</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></span></div></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Hope that helps.</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">- Dave Sweeris</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div><div><blockquote type="cite" class=""><div class="">On Apr 18, 2016, at 1:42 PM, Peter Eddy 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=""><div dir="ltr" class="">Hello,<div class=""><br class=""></div><div class="">I'd like to understand why I'm unable to use the nil coalescing operator with functions.</div><div class=""><br class=""></div><div class="">For example, I'd like to write a function that takes an optional function as a parameter and that uses the nil coalescing operator to select a default function if the function's function parameter is nil, e.g:</div><div class=""><br class=""></div><div class=""><div class=""> func test(x: ((String) -> String)? = nil) {</div><div class=""><br class=""></div><div class=""> let qq = { (p:String) -> String in return "..." }</div><div class=""><br class=""></div><div class=""> let fn: String -> String = x ?? qq</div><div class=""><br class=""></div><div class=""> fn("test")</div><div class=""> }</div></div><div class=""><br class=""></div><div class="">When I do this the compiler complains that:</div><div class=""><br class=""></div><div class=""> Binary operator '??' cannot be applied to operands of type '((String) -> String)?' and '(String) -> String:</div><div class=""><br class=""></div><div class="">In the Swift 2.2 Language Guide (<a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html" class="">https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html</a>) it states:</div><div class=""><br class=""></div><div class=""><p class="" style="background-color:transparent;border:0px;font-size:1.4em;margin:0px 0px 15px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(65,65,65);line-height:20.299999237060547px;font-family:Helvetica,Arial,sans-serif">The nil coalescing operator is shorthand for the code below:</p><div class="" style="background-color:transparent;border:0px;font-size:1.4em;margin:0px 0px 0px 21px;outline:0px;padding:0px;vertical-align:baseline"><div class="" style="background-color:transparent;border-width:0px 0px 0px 5px;border-left-style:solid;border-left-color:rgb(205,233,244);margin:10px 0px 21px 13px;outline:0px;padding:4px 0px 2px 10px;vertical-align:baseline;width:616.578125px"><ol class="" style="background-color:transparent;border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;line-height:1.6em;list-style:none"><li style="background-color:transparent;border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;color:rgb(255,255,255);list-style-type:none" class=""><code class="" style="background-color: transparent; border: 0px; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; font-family: Menlo, monospace; word-wrap: break-word;"><span class="" style="background-color:transparent;border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">a</span> != <span class="" style="background-color:transparent;border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">nil</span> ? <span class="" style="background-color:transparent;border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">a</span>! : <span class="" style="background-color:transparent;border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">b</span></code></li></ol></div></div></div><div class="">If I use the longhand option then the complier's fine:</div><div class=""><br class=""></div><div class=""> func test(x: ((String) -> String)? = nil) {<br class=""></div><div class=""> </div><div class=""> let qq = { (p:String) -> String in return "..." }</div><div class=""> </div><div class=""> let fn = x != nil ? x! : qq</div><div class=""><br class=""></div><div class=""> fn("test")</div><div class=""> }</div><div class=""><br class=""></div><div class="">So why doesn't the nil coalescing operator work for function vars? </div><div class=""><br class=""></div><div class="">thanks!</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>