Open MahdiBM opened 5 months ago
@AnthonyLatsis Property wrappers are only applied when the property is accessed or modified, and they don't fundamentally change the behavior of properties themselves.
So, in essence, the compiler doesn't enforce the initialization requirement for _name
because it's not being accessed or modified in a way that triggers the property wrapper's logic. This behavior might seem unexpected, but it's consistent with how property wrappers are designed to work in Swift.
Correct me if I misunderstood.
@Rajveer100 there are no property wrappers here though.
@storageRestrictions
is an attribute to help the compiler in enforcing stuff like definite initialization in init
accessors.
The rest is also unrelated to property wrappers, in this certain issue.
I see, my bad 🙂.
I think you are mixing up property wrappers with init
accessors. An init
accessor must initialize (set) the properties listed in the initializes
argument of the attached storageRestrictions
attribute. The body of the accessor, self._name = newValue?.description
, is an unconditional assignment to _name
, so I, too, expect this code to compile successfully.
But the newValue
may not have been initialised in the first place right, so accessing the description
would not make sense?
newValue
is the init
accessor’s implicit parameter, like in regular set
accessors. It cannot be uninitialized.
Description
See Reproduction.
self._name = newValue?.description
does not compile presumably because it modifies the optional value, but something likeself._name = newValue
does compile.Reproduction
Expected behavior
No errors.
Environment
Tested on Swift 5.10 and Swift 6 nightly docker images.
Additional information
Workaround: