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

Subclass existential doesn't satisfy generic class constraints

    XMLWordPrintable

    Details

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

      Xcode 9.2

      Apple Swift version 4.1 (swiftlang-902.0.34 clang-902.0.30)

      Description

      Example:

       

      class AClass {}
      protocol AProto { var value: Int { get } }
      
      class Box<T> { 
        private var _boxed: T
        init(_ b: T) { _boxed = b } 
      }
      extension Box where T: AClass { 
        var boxed: T { 
          get { return _boxed }
          set { _boxed = newValue } 
        } 
      }
      
      class One: AClass, AProto { var value: Int { return 1 } }
      class Two: AClass, AProto { var value: Int { return 2 } }
      
      let a: Box<AClass & AProto> = Box(One())
      print(a.boxed) // error: 'AClass & AProto' is not convertible to 'AnyObject'
      a.boxed = Two()
      print(a.boxed)
      
      let b: AClass & AProto = One()
      print(b is AClass) // 'true'

       

      See discussion at: https://forums.swift.org/t/confused-by-generic-behaviour-not-clear-if-it-is-a-bug/9823

       

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              kawa Karl
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: