Skip to content

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Jun 6, 2023

Cherry-pick of #66283


  • Adds a new kind of accessor - init accessor - which could be used to initialize one or more stored properties of the type.

    • Just like getter and setter accessors, init accessor could be used on any computed property.

    • Init accessors can specify a set of properties they initialize and accesses (property that should be initialized already) via declaration attributes - initializes and accesses;

    • Example of init accessor that initializes a stored property:

       public struct Test { 
         private var _a: Int
      
         public var a: Int {
           init(initialValue) initializes(_a) {
             _a = initialValue
           }
      
           get { _a }
           set { _a = newValue }
        }
      }
  • Modifies memberwise initializers to take advantage of this new mechanism by replacing one or more stored property parameters with corresponding init accessor(s).

    • struct Test from previous example would get init(a: Int) { self.a = a }.
  • Adds a new SIL instruction - assign_or_init; Similar to assign_by_wrapper the "mode" is set by DI based on each use site.

    • New intruction also tracks "assignments" - fields that have been previously initialized and would re-assigned by invocation of init accessor, it enables as to call init accessors multiple times even before all fields have been initializes.
  • When used in an initializer body, init accessor (i.e. self.a = a) call becomes either an initialization (if all of the fields haven't been initialized yet), or a call to setter.

@xedin xedin added 🍒 release cherry pick Flag: Release branch cherry picks swift 5.9 labels Jun 6, 2023
@xedin xedin requested a review from hborla June 6, 2023 17:11
@xedin xedin requested a review from a team as a code owner June 6, 2023 17:11
@xedin
Copy link
Contributor Author

xedin commented Jun 7, 2023

@swift-ci please test

@xedin xedin force-pushed the init-accessors-5.9 branch from 6a4d38c to 2b75a06 Compare June 7, 2023 01:55
@xedin
Copy link
Contributor Author

xedin commented Jun 7, 2023

@swift-ci please test

- Adds a new kind of accessor - init accessor - which could be used
  to initialize one or more stored properties of the type.
  - Just like getter and setter accessors, init accessor could be used
    on any computed property.
  - Init accessors can specify a set of properties they initialize and
    accesses (property that should be initialized already) via declaration
    attributes - `initializes` and `accesses`;

  - Example of init accessor that initializes a stored property:
    ```swift
    public struct Test {
      private var _a: Int

      public var a: Int {
        init(initialValue) initializes(_a) {
          _a = initialValue
        }

        get { _a }
        set { _a = newValue }
      }
    }
    ```

- Modifies memberwise initializers to take advantage of this new
  mechanism by replacing one or more stored property parameters with
  corresponding init accessor(s).
  - `struct Test` from previous example would get `init(a: Int) { self.a = a }`.

- Adds a new SIL instruction - `assign_or_init`; Similar to `assign_by_wrapper`
  the "mode" is set by DI based on each use site.
  - New intruction also tracks "assignments" - fields that have been
    previously initialized and would re-assigned by invocation of init
    accessor, it enables as to call init accessors multiple times even
    before all fields have been initializes.

- When used in an initializer body, init accessor (i.e. `self.a = a`)
  call becomes either an initialization (if all of the fields haven't
  been initialized yet), or a call to setter.
@xedin xedin force-pushed the init-accessors-5.9 branch from 2b75a06 to 720b89d Compare June 7, 2023 15:21
@xedin
Copy link
Contributor Author

xedin commented Jun 7, 2023

@swift-ci please test

@xedin xedin merged commit c95d3e2 into swiftlang:release/5.9 Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🍒 release cherry pick Flag: Release branch cherry picks swift 5.9

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants