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

'self' in property initial value expression refers to class (?)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      macOS 10.12.4, Xcode 8.3.1

      Description

      Given a function f that takes Any in one of its parameters, passing self in a call to f is allowed in a closure that initializes a let property.

      The compiler should raise an issue for using self before all stored properties are initialized and the issue should disappear when the property is transformed to a lazy var. But the current behavior is that self is casted as func `self`() -> Self and the compiler does not complain.

      The following code reproduces the issue:

      import UIKit
      
      class A: NSObject {
      
        private let control: UIControl = {
          print("I'm not lazy")
          let control = UIControl()
          control.addTarget(self, action: #selector(valueChanged), for: .valueChanged)
          print(self) // prints "(Function)"
          doSomething(with: self)
          return control
        }()
      
      
        @objc private func valueChanged() {
      
        }
      }
      
      func doSomething(with a: Any) {
        print("called 'Do Something'")
      }
      
      A()
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              mouhcine Mouhcine El Amine
              Votes:
              6 Vote for this issue
              Watchers:
              13 Start watching this issue

                Dates

                Created:
                Updated: