Rather than explaining the problem I'm going to paste a piece of code exposing it:
@propertyWrapper
struct IdentityWrapper {
var wrappedValue: Int
init() {
self.wrappedValue = 0
}
init(wrappedValue: Int) {
self.wrappedValue = wrappedValue
}
}
struct Data {
@IdentityWrapper var id: Int
}
let d1 = Data(id: IdentityWrapper(wrappedValue: 10))
struct Data2 {
@IdentityWrapper var id: Int = 0
}
let d2 = Data2(id: 10)
As you can see in the case of Data the synthesized initializer has a parameter of the type of the property wrapper, and in the case of Data2 the sysnthesized initializer has a paratemer of the type of the wrapped value.
I've searched the forums and I've reached this Swift evolution proposal specifying when this would happen, and it clearly states that when the property wrapper has an initializer with a wrappedValue parameter, the synthesized intitializer should use the type of the wrapped value. This is not the case if you have an initializer with no parameters in the property wrapper (either explicit or synthesized). Is this a bug?
(BTW, I've found several related discussions with no clear answer, at least that I've been able to find)
In fact, reading the proposal documentation, the conditions for using the property wrapper type vs the original type seem to be redundant. Unless I am missing something, there is no way to assign an initial value to a wrapped property unless the property wrapper type has an init(wrappedValue:), is there? So the decision to use the original type would be reduced to finding out if the property wrapper has that initializer.