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

Protocols extending class types are not recognized as class types

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Duplicate
    • Component/s: Compiler
    • Labels:
    • Environment:

      Swift compiler 4.2-RELEASE

      Description

      class Alpha<T> where T: AnyObject {
          private let value: T
          init(_ value: T) {
              self.value = value
          }
      }
      
      protocol Beta: AnyObject {}
      
      class Gamma: Beta {}
      
      print(Alpha<Beta>(Gamma()))

       

      Expected:

      • The above code should compile: The requirement on generic `T` is that is that it extends from `AnyObject`. Since protocol `Beta` also extends from `AnyObject` it holds that any class `Foo` that implements protocol `Beta` will also extend from `AnyObject`

      Actual:

      • Code does not compile. Error message is error: "'Alpha' requires that 'Beta' be a class type"

      Note: `class` and `AnyObject` are type-aliased to the same thing, so changing to `Beta: class` produces the same error

       

      Also, this works as expected:

      class Alpha<T> where T: AnyObject {
          private let value: T
          init(_ value: T) {
              self.value = value
          }
      }
      class Beta {}
      class Gamma: Beta {}
      print(Alpha<AnyObject>(Gamma())) 

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              jbfit Jason B
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: