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

Custom initializers always call member initializers

    XMLWordPrintable

    Details

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

      Description

      During SE-0242 (https://forums.swift.org/t/se-0242-synthesize-default-values-for-the-memberwise-initializer/20618/1), there was discussion about how the compiler currently treats initializers for structs and classes.

      func zero() -> Int {
        print("hello")
        return 0
      }
      
      struct X {
        var y = zero()
      
        init(y: Int) {
          self.y = y
        }
      }
      
      let x = X(y: 10) // hello
      

      This behavior does not occur with memberwise initializers because they are lowered differently, but user defined initializers all have this same behavior.

      Accepting rationale stated that the core team felt this behavior was a bug
      (https://forums.swift.org/t/se-0242-synthesize-default-values-for-the-memberwise-initializer/20618/98).

      This is a somewhat complicated issue and I plan to write a much longer topic on the forums discussing this and getting some feedback there. I'll post the link here when I get done writing it.

        Attachments

          Activity

            People

            • Assignee:
              Azoy Alejandro Alonso
              Reporter:
              Azoy Alejandro Alonso
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: