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

@objc protocol methods cannot return Self

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
      None

      Description

      In Swift, the following is permitted:

      protocol Foo {
          static func makeFoo() -> Self
      }
      

      But if the protocol is marked as @objc, it generates the warning "Method cannot be a member of an @objc protocol because its result type cannot be represented in Objective-C"

      However, adding Self as a return type to a protocol method doesn't usually treat Self as an associatedType requirement, so it's not clear why a protocol method returning Self can't be represented in Objective-C.

      The equivalent Objective-C code is permitted and works as you'd expect:

      @protocol Foo
      + (instancetype)makeFoo;
      @end
      
      @interface Bar: NSObject<Foo>
      @end
      
      @implementation Bar
      + (instancetype)makeFoo { return [[self alloc] init]; }
      @end
      

        Attachments

          Activity

            People

            • Assignee:
              suyashsrijan Suyash Srijan
              Reporter:
              nicklockwood Nick Lockwood
            • Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: