Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3349] @objc methods cannot be contained in protocol extensions #45937

Closed
phausler opened this issue Dec 6, 2016 · 2 comments
Closed

[SR-3349] @objc methods cannot be contained in protocol extensions #45937

phausler opened this issue Dec 6, 2016 · 2 comments
Labels
bug A deviation from expected or documented behavior Compiler

Comments

@phausler
Copy link
Member

phausler commented Dec 6, 2016

Previous ID SR-3349
Radar None
Original Reporter @phausler
Type Bug
Status Closed
Resolution Won't Do
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: c37f7a5814c6781d85ad8f6de65d8fec

is duplicated by:

  • SR-4744 @objc Attribute validation should support Protocol Extension declaration

relates to:

  • SR-802 Cannot implement objc protocol extension method
  • SR-7301 Bad fixit suggested for protocol extension implementation for inherited objc protocol

Issue Description:

Given a swift protocol, extensions can be made conditionally upon Self being a specific type. That specific type can be declared as NSObject. So it stands to reason that the methods declared in the extension (where Self is a derivative of NSObject) should be isomorphic to extensions on the type itself. Extensions on NSObject derivatives can contain @objc declarations. Conditional protocol extensions however are specifically disallowed from this pattern (which seems inconsistent).

import Foundation

public protocol Bar {
    func baz()
}


extension Bar where Self: NSObject {
    @objc(foo) // <- @objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
    func foo() {
        baz()
    }
}

class MyObject : NSObject, Bar {
    func baz() { }
}

let m = MyObject()
m.foo()

The restriction limits the capability of making interoperation between swift protocols and objective-c adopters that might be passed back into objc layers; which might prefer some mechanism for maintaining that interoperability. Abjectly disallowing this prevents this interoperability and impedes more appropriate swift behavior.

@belkadan
Copy link
Contributor

belkadan commented Dec 9, 2016

This is intended. There is no way to add the implementation of the method to every adopter, since the extension could be added after the conformance to the protocol. I suppose we could allow it if the extension is in the same module as the protocol, though.

@DougGregor
Copy link
Member

DougGregor commented Feb 14, 2017

As Jordan notes, this is intentional: protocol extensions cannot introduce @objc entry points due to limitations of the Objective-C runtime. If you want to add @objc entry points to NSObject, extend NSObject.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior Compiler
Projects
None yet
Development

No branches or pull requests

3 participants