[swift-users] Nil coalescing operator (??) and function vars

Chris Lattner clattner at apple.com
Tue Apr 19 16:35:35 CDT 2016


> On Apr 18, 2016, at 11:42 AM, Peter Eddy via swift-users <swift-users at swift.org> wrote:
> 
> Hello,
> 
> I'd like to understand why I'm unable to use the nil coalescing operator with functions.
> 
> 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:
> 
>     func test(x: ((String) -> String)? = nil) {
> 
>       let qq = { (p:String) -> String in return "..." }
> 
>       let fn: String -> String = x ?? qq
> 
>       fn("test")
>     }
> 
> When I do this the compiler complains that:
> 
>   Binary operator '??' cannot be applied to operands of type '((String) -> String)?' and '(String) -> String:
> 
> In the Swift 2.2 Language Guide (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html <https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html>) it states:

This is a bug, which has already been fixed in master.  The fix will go out with Swift 3.

-Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160419/a711e586/attachment.html>


More information about the swift-users mailing list