Nil coalescing operator and closures


(Rudolf Adamkovič) #1

Hello there!

Today, I stumbled upon the following error:

typealias ClosureType = () -> Void
        
let closure: ClosureType?
let otherClosure: ClosureType = {}
        
let pickClosure = closure ?? otherClosure
// ERROR: binary operator '??' cannot be applied to operands of type 'ClosureType?' and 'ClosureType' (aka '() -> ()')

Is this a bug or a feature?

Thanks!

Rudolf Adamkovic
R+


(David Sweeris) #2

Assuming you've merely omitted the code where you set closure to something and aren’t trying to read a variable before initializing it, I believe it's a bug... Using the "explicit" version works fine:
let closure: Closure? = nil
let otherClosure: Closure = {}
let pickClosure = closure != nil ? closure! : otherClosure

- Dave Sweeris

···

On Feb 24, 2016, at 7:37 AM, Rudolf Adamkovič via swift-users <swift-users@swift.org> wrote:

Hello there!

Today, I stumbled upon the following error:

typealias ClosureType = () -> Void
        
let closure: ClosureType?
let otherClosure: ClosureType = {}
        
let pickClosure = closure ?? otherClosure
// ERROR: binary operator '??' cannot be applied to operands of type 'ClosureType?' and 'ClosureType' (aka '() -> ()')

Is this a bug or a feature?

Thanks!

Rudolf Adamkovic
R+

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Joe Groff) #3

Hello there!

Today, I stumbled upon the following error:

typealias ClosureType = () -> Void
        
let closure: ClosureType?
let otherClosure: ClosureType = {}
        
let pickClosure = closure ?? otherClosure
// ERROR: binary operator '??' cannot be applied to operands of type 'ClosureType?' and 'ClosureType' (aka '() -> ()')

Is this a bug or a feature?

It's a known bug.

-Joe

···

On Feb 24, 2016, at 5:37 AM, Rudolf Adamkovič via swift-users <swift-users@swift.org> wrote:

Thanks!

Rudolf Adamkovic
R+

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rudolf Adamkovič) #4

Thanks for letting me know!

R+

···

Sent from my iPhone

On 24 Feb 2016, at 18:54, Joe Groff <jgroff@apple.com> wrote:

On Feb 24, 2016, at 5:37 AM, Rudolf Adamkovič via swift-users <swift-users@swift.org> wrote:

Hello there!

Today, I stumbled upon the following error:

typealias ClosureType = () -> Void
        
let closure: ClosureType?
let otherClosure: ClosureType = {}
        
let pickClosure = closure ?? otherClosure
// ERROR: binary operator '??' cannot be applied to operands of type 'ClosureType?' and 'ClosureType' (aka '() -> ()')

Is this a bug or a feature?

It's a known bug.

-Joe

Thanks!

Rudolf Adamkovic
R+

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users