You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
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.
Additional Detail from JIRA
md5: c37f7a5814c6781d85ad8f6de65d8fec
is duplicated by:
relates to:
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).
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.
The text was updated successfully, but these errors were encountered: