swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.32k stars 10.34k forks source link

[SR-12216] A callAsFunction type should be accepted everywhere a closure is #54642

Open davedelong opened 4 years ago

davedelong commented 4 years ago
Previous ID SR-12216
Radar rdar://problem/59496052
Original Reporter @davedelong
Type Bug
Environment Xcode 11.4 beta 1 on macOS Catalina 10.15.3
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 18260971602ef5ab0897e8edc3b45ff0

Issue Description:

I was disappointed to find out that this doesn't work:

struct Print {
    func callAsFunction() -> Void {
        print("hello, world!")
    }
}

func doThing(_ thing: () -> Void) {
    thing()
}

let p = Print()
p() // prints "hello, world!"

doThing(p) // compiler error

Since the syntax for invoking a function/closure is the same syntax as invoking a type that has a callAsFunction method, I should be able to use a callAsFunction type anywhere I can use a closure.

davedelong commented 4 years ago

I just found that this limitation is under the "Future Directions" part of the original proposal: https://github.com/apple/swift-evolution/blob/master/proposals/0253-callable.md#implicit-conversions-to-function

beccadax commented 4 years ago

@swift-ci create

vanvoorden commented 2 months ago

@grynspan I actually just ran into this trying to pass a Testing.Confirmation instance directly to a function that expected a closure.

grynspan commented 2 months ago

Okay?