[swift-evolution] Pitch: Deprecate/remove AnyObject method dispatch

BJ Homer bjhomer at gmail.com
Wed Oct 25 10:27:59 CDT 2017


That would be a Cocoa-side change, since UIView, NSView, NSMenuItem, NSCell are all Cocoa-level objects. (I’m not aware of any other type that would be commonly passed as a ‘sender’ parameter and would have a tag.) That change wouldn’t go through Swift-Evolution.

So in theory, if Swift 5 decides to get rid of the AnyObject dispatch, the Cocoa teams at Apple could work around that by introducing “@objc protocol IntegerTagged”, and introducing a migrator pass to look for '(sender as AnyObject).tag’ and rewrite it as `(sender as! IntegerTagged)?.tag.

If the ‘tag’ property is the only reason to keep AnyObject dispatch, I don’t think it’s worth its weight in the language.

-BJ

> On Oct 25, 2017, at 8:28 AM, Charles Srstka via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I guess, but doesn’t it seem far more elegant to have a protocol for tag-containing objects? This is a feature that’s pretty heavily used…
> 
> Charles
> 
>> On Oct 24, 2017, at 6:06 PM, Slava Pestov <spestov at apple.com <mailto:spestov at apple.com>> wrote:
>> 
>> You can implement an @objc protocol with an optional requirement, and make NSObject conform to it.
>> 
>> Slava
>> 
>>> On Oct 24, 2017, at 4:05 PM, Charles Srstka <cocoadev at charlessoft.com <mailto:cocoadev at charlessoft.com>> wrote:
>>> 
>>>> On Oct 24, 2017, at 5:02 PM, Slava Pestov via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> 
>>>> Thoughts? Does anyone actually rely on this feature, instead of just stumbling on it by accident once in a while?
>>> 
>>> The main thing I can think of off the top of my head is getting the tag from the sender in an IBAction:
>>> 
>>> @IBAction private func someAction(_ sender: Any?) {
>>>     guard let tag = (sender as AnyObject?)?.tag as Int? else { return }
>>> 
>>>     ...
>>> }
>>> 
>>> Unfortunately given how many unrelated Cocoa objects there are that implement -tag, it’s not really practical to implement this without the AnyObject dispatch. If a TagContaining protocol could be introduced and all the objects that implement -tag could be made to conform to it, then that would work around the problem (I believe I pitched this at some point long ago, but without catching any interest).
>>> 
>>> Charles
>>> 
>> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171025/6d2bc4ef/attachment.html>


More information about the swift-evolution mailing list