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

Generics with Self on class type failing compiler

    XMLWordPrintable

    Details

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

      Description

      See forum thread for more details:

      https://forums.swift.org/t/property-wrappers-access-to-both-enclosing-self-and-wrapper-instance/32526

      This property wrapper compiles, but not when used:

      @propertyWrapper
      public final class Wrapper {
          
          public static subscript<EnclosingSelf>(
            _enclosingInstance observed: EnclosingSelf,
            wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Int?>,
            storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
          ) -> Int? {
            get {
              return observed[keyPath: storageKeyPath].stored
            }
            set {
              let oldValue = observed[keyPath: storageKeyPath].stored
              if newValue != oldValue {
                  // TODO: call wrapper instance with enclosing self
              }
              observed[keyPath: storageKeyPath].stored = newValue
            }
          }
          
          public var wrappedValue: Int? {
            get { fatalError("called wrappedValue getter") }
            set { fatalError("called wrappedValue setter") }
          }
          
          public init(str: String) {
              self.str = str
          }
          
          // MARK: - Private
          
          private let str: String
          private var stored: Int?
      }
      

      When used, the compiler fails:

      open class TestView: UIView {
          @Wrapper(str: "HelloWorld") public var testProp: Int?
      }
      
      /** 
          Swift Compiler Error
          Type '_' has no member 'testProp'
      */
      

      Replacing `Self` with `Wrapper` in the code fixes the issue. If I keep `Self`, I can also change the class to a struct to fix the issue.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              ebg Erich Graham
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: