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-857] Extensions to generic classes cannot implement Objective-C protocols #43469

Open
swift-ci opened this issue Mar 2, 2016 · 4 comments
Labels
compiler The Swift compiler in itself feature A feature request or implementation

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 2, 2016

Previous ID SR-857
Radar None
Original Reporter benasher44 (JIRA User)
Type Bug
Environment

Swift 2.1
Mac OS X 10.11.3
Xcode 7.2.1

Additional Detail from JIRA
Votes 8
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee None
Priority Medium

md5: 2de02e9c18e4d7298b9946f1b00e3d94

duplicates:

  • SR-7370 Generic class conforming to @objc protocol fails assertion: "extended objc class doesn't have constant metadata?"

Issue Description:

Let's say you have a protocol and a class like the following:

@protocol MyProtocol <NSObject>
- (void)foo;
@end
class MySubclass: MyGenericSuperClass<SpecificType> {
}

If I want to make MySubclass implement MyProtocol, my first attempt would be something like this:

extension MySubclass: MyProtocol {
  func foo() -> Void {
  }
}

But, this yields a bunch of strange compiler errors. First, it says I need to qualify foo as @objc. Okay, so I do that, and then it tells me that @objc is not supported within extensions of generic classes. However, it's perfectly okay to do the following:

class MySubclass: MyGenericSuperClass<SpecificType>, MyProtocol {
  func foo() -> Void {
  }
}

If I define my class like that instead, it compiles and everything works as expected. The @objc limitation seems a bit odd, given that I can just move the conformance out of the extension and back into the class and everything works just fine.

What I'd like to say changed:

  • Allow extensions to implement Objective-C protocols in generic classes, so that we don't have to stray from the "implement your protocols in extensions" patterns for just this case

  • At the very least, I think the compiler should just tell you up front: "You can't implement Objective-C protocols in extensions to generic classes"

@belkadan
Copy link
Contributor

belkadan commented Mar 8, 2016

Both good ideas. @slavapestov, it's not really possible to add @objc methods to generic classes from another module, right? (But we could allow it within the same module.)

@swift-ci
Copy link
Collaborator Author

swift-ci commented Nov 2, 2017

Comment by Kaden Wilkinson (JIRA)

Also hit this today and would love to be able to use extensions to clean up some files

@swift-ci
Copy link
Collaborator Author

swift-ci commented Mar 7, 2018

Comment by Olaf Andreas Øvrum (JIRA)

The last idea, Yes! I rewrote a lot of code after which I was met by the compiler letting me know: "@objc is not supported within extensions of generic classes or classes that inherit from generic classes". Life is hard.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

The diagnostics QoI issue is fixed. Repurposing this report into a request to allow @objc in extensions of generic classes within the same module.

@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation and removed bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. diagnostics QoI Bug: Diagnostics Quality of Implementation labels Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation
Projects
None yet
Development

No branches or pull requests

3 participants