[swift-evolution] [Idea] Expression to retrieve the Objective-C selector of a method

Joe Groff jgroff at apple.com
Mon Dec 28 12:49:22 CST 2015


> On Dec 28, 2015, at 10:46 AM, FĂ©lix Cloutier <felixcca at yahoo.ca> wrote:
> 
> Would it be possible to add properties to closures/method objects?
> 
> Right now, it's possible to write:
> 
>> @objc
>> class Foo : NSObject {
>> 	func doSomething(value: Int) -> Int {
>> 		return value + 1
>> 	}
>> }
>> 
>> let method = Foo.doSomething
> 
> Maybe we could have Foo.doSomething.selector for @objc methods?

Once you have a normal function value, it's not guaranteed you can recover any identity from it, since an arbitrary T -> U doesn't necessarily correspond to an @objc method. I'm proposing we do this by type context, similar to how we form C function pointers and blocks. Just like you can do:

let fptr: @convention(c) (Double) -> Double = sqrt

to get the function pointer for `sqrt`, you could say:

let sel: Selector = Foo.doSomething

to get the selector for `doSomething`.

-Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151228/cfa1cc2f/attachment.html>


More information about the swift-evolution mailing list