I'm noticing a weird issue with a property wrapper where the designated initializer of a type using the property wrapper changes depending on how an optional value is initialised.
For example, given the following property wrapper:
@propertyWrapper
struct ExampleProperty<T> {
var wrappedValue: T?
}
And this type using it:
struct SomeType {
@ExampleProperty var foo: Int? = nil
}
The generated memberwise initializer for SomeType
is init(foo: Int?)
. That's fine, but if I don't explicitly initialize foo to nil
:
struct SomeType {
@ExampleProperty var foo: Int?
}
The memberwise initializer is now init(foo: ExampleValue<Int>)
which isn't what I'd expect or want. I spotted this bug because we are using SwiftLint and one of its default rules is to strip redundant optional initialisation.
It gets weirder if you have a property wrapper that takes an additional parameter. This time, I've given the property wrapper an explicit public initializer, as I would if it was in a library:
@propertyWrapper
struct ExampleProperty<T> {
var wrappedValue: T?
var other: String
public init(wrappedValue: T?, other: String) {
self.wrappedValue = wrappedValue
self.other = other
}
}
This time, @ExampleProperty var foo: Int?
won't even compile, complaining about missing an argument for property wrappedValue
- again, I can fix this in one of two ways - 1) explicitly doing var foo:Int? = nil
or giving the wrappedValue
parameter in the property wrapper's init
a default value of nil
- this also works but again causes the problem of changing the memberwise initializer in an unexpected way.
For now it looks like my only solution is to disable the SwiftLint default rule and explicitly add = nil
at the property declaration but I'm confused about why this is necessary and why the other solutions affect the memberwise initializer.