I would add to what Joe mentioned above the fact that the concept of "selector" may not mean a whole lot to developers who are first introduced to Swift without any prior Obj-C or Cocoa experience. Thinking of them as functions I believe avoids introducing complexity in the form of additional concepts that one must understand and differentiate ("what's a selector and why/how is it different from a function value?")<br><div class="gmail_quote"><div dir="ltr">On Sun, Dec 27, 2015 at 10:07 AM Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
> On Dec 26, 2015, at 11:48 PM, Douglas Gregor via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
><br>
> Hi all,<br>
><br>
> Currently, producing an Objective-C selector in Swift is an error-prone operation. One effectively just writes a string literal and uses it in a context where an ObjectiveC.Selector is expected:<br>
><br>
> control.sendAction(“doSomething:”, to: target, forEvent: event)<br>
><br>
> There are many points of failure here:<br>
><br>
> 1) The compiler doesn’t syntax-check at all to make sure it’s a valid spelling for a selector<br>
> 2) The compiler doesn’t look for existing methods with this selector anywhere<br>
> 3) The mapping from a Swift method name to an Objective-C selector isn’t always immediately obvious (especially for initializers), and will be getting significantly more complicated with the renaming work for Swift 3 (<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0005-objective-c-name-translation.md" rel="noreferrer" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/0005-objective-c-name-translation.md</a>).<br>
><br>
> I suggest that we add an expression ‘objc_selector(method-reference)` that produces the Objective-C selector for the named method, and produces an error if the method does not have an Objective-C entry point. For example:<br>
><br>
> control.sendAction(objc_selector(MyApplication.doSomething), to: target, forEvent: event)<br>
><br>
> “doSomething” is a method of MyApplication, which might even have a completely-unrelated name in Objective-C:<br>
><br>
> extension MyApplication {<br>
> @objc(jumpUpAndDown:)<br>
> func doSomething(sender: AnyObject?) { … }<br>
> }<br>
><br>
> By naming the Swift method and having objc_selector do the work to form the Objective-C selector, we free the programming from having to do the naming translation manually and get static checking that the method exists and is exposed to Objective-C.<br>
><br>
> This proposal composes with my “Generalized Naming for Any Function” proposal, which lets us name methods fully, including getters/setters:<br>
><br>
> let sel1: Selector = objc_selector(UIView.`insertSubview(_:at:)`) // produces the Selector “insertSubview:atIndex:"<br>
> let sel2: Selector = objc_selector(UIView.`frame.get`) // produces the Selector “frame"<br>
><br>
> I don’t like the `objc_selector` syntax at all, but otherwise I think this functionality is straightforward.<br>
<br>
Selectors can be seen as "just" a kind of function value. Do we need a new syntax form at all? We ought to be able to turn an unbound function reference like UIView.insertSubview into a selector reference in Selector type context, or maybe a typed @convention(selector) function as discussed in another thread, without any explicit get-a-selector operation.<br>
<br>
-Joe<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div><div dir="ltr">-- <br></div>Javier Soto