Skip to content

init accessors. #2042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 14, 2023
Merged

init accessors. #2042

merged 13 commits into from
Jun 14, 2023

Conversation

hborla
Copy link
Member

@hborla hborla commented May 9, 2023

hborla and others added 3 commits May 5, 2023 15:09
* Expand description of accesses and give a "dictionary storage" example

* Make the name of the `newValue` parameter configurable

Be consistent with set/willSet/didSet in making the parameter name
optional and overridable.

* A few clarifications regarding init accessor signatures

* Clarify the rules for definite initialization

Expand the examples and show the case where virtual and stored-property
initialization collide.

---------

Co-authored-by: Holly Borla <[email protected]>
@hborla hborla changed the title Init accessors. init accessors. May 9, 2023
self.point = (x, y) // error: neither the `init` accessor nor the setter can be called here
}
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A case that I don't think is covered here: what about a computed property with an init accessor which is conditionally initialized along some path, and then reassigned?

struct S {
    var x: Int {
        init initializes(y) {
            y = newValue
        }
        get { ... }
        set { ... }
    }
    var y: Int

    init(x: Int, y: Int) {
        if y > 0 {
            self.x = 0
        }

        self.x = x
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An assignment to a property that is not initialized on all paths will call the init accessor again. This is a generalization of what property wrappers do. I can add this example to the proposal.


### Memberwise initializers

If a struct does not declare its own initializers, it receives an implicit memberwise initializer based on the stored properties of the struct, because the storage is what needs to be initialized. Because `init` provide a preferred mechanism for initializing storage, the memberwise initializer parameter list will include any computed properties that subsume the initialization of stored properties instead of parameters for those stored properties.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because init provide a preferred mechanism for initializing storage, the memberwise initializer parameter list will include any computed properties that subsume the initialization of stored properties instead of parameters for those stored properties.

I think this is a perfectly reasonable position, but I wonder if the notion that init accessors represent the preferred initialization strategy deserves mention as part of Proposed solution rather than just being a side mention in a subsection of Detailed design. That's a not-insignificant policy decision about the feature!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concretely, I think the "preferred mechanism" thing (and its relationship to the memberwise init) is the only thing in this proposal that casts doubt on what otherwise seems a perfectly reasonable use of init accessors: having multiple computed properties which initialize the same underlying storage, e.g., radians and rotations both initializing degrees.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added more commentary in the Proposed Solution and in this section about member-wise initializers.

Co-authored-by: Frederick Kellison-Linn <[email protected]>
@hborla hborla force-pushed the init-accessors branch 2 times, most recently from 6fe4a46 to 5ccb0bb Compare June 12, 2023 23:53
@hborla hborla marked this pull request as ready for review June 13, 2023 00:47
@hborla
Copy link
Member Author

hborla commented Jun 13, 2023

@Jumhyn I believe I've addressed all of your feedback, and I strengthened the Alternatives Considered section with some suggestions from the pitch thread. Please let me know if you have any further feedback!

@Jumhyn Jumhyn merged commit 46fa1ee into swiftlang:main Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants