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

Generics don't identify Protocol with class requirement properly

    XMLWordPrintable

    Details

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

      Xcode 9.4.1 production on Swift 4.1.

      Description

      When you make a generic that requires that the generic type inherit from AnyObject (be a class), it is not satisfied if you use a protocol that is constrained to only class type. Here is example code.

      class WeakReference<T: AnyObject> {
          private(set) weak var value: T?
      
          init(_ value: T?) {
              self.value = value
          }
      }
      
      public protocol TempoSetter: class {
          var currentTempo: Int { get set }
      }
      
      class ABC {
          var tempoSetters: Set<WeakReference<TempoSetter>> = []
          init() {}
      }
      

      Expected

      This compiles

      Actual:

      error: 'WeakReference' requires that 'TempoSetter' be a class type
          var tempoSetters: Set<WeakReference<TempoSetter>> = []
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              drumnkyle Kyle Sherman
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: