Uploaded image for project: 'Swift'
  1. Swift
  2. SR-857

Extensions to generic classes cannot implement Objective-C protocols

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
    • Environment:

      Swift 2.1
      Mac OS X 10.11.3
      Xcode 7.2.1

      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"

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              benasher44 Ben A
              Votes:
              8 Vote for this issue
              Watchers:
              9 Start watching this issue

                Dates

                Created:
                Updated: