Enable let-constant property with default value to be a part of memberwise initializers

I guess it is a duplicate.

I would like to know why Swift doesn't generate memberwise initializers for structures with let-constants properties with default values.

Without following initializer definition I can't take advantages from default values for let-properties and from memberwise initializers.

struct SomeStruct {
    let value: String
    init(value: String = .init()) {
        self.value = value
    static func abc() {

Is there a pitch/implementation for this? ( I would like if it is already implemented and waiting us in Swift 5.2 )

This is because it's not the default value, but rather the initial value. You have already defined a constant with a value and there is no way to change it.

struct A {
  let value: Int = 0

Here, value cannot be overwritten, so there's no point in generating a memberwise initializer for A. Now, if you mean that the compiler should move the initial value into the initializer implicitly, then that's a different thing.


This topic was discussed heavily during the SE-0018 process. The conclusion of that was that there is some possibility of changing the language such that let properties could have default values. The most recent discussion in this area was Explicit Memberwise Initializers. All work in this area is stalled until somebody is willing and able to work on compiler implementation.


Well, then there are double standards here.
Let's say we define a "var" property with "didSet" and give it an initial value (as we remember, "didSet" doesn't fire on initialization).
Then we write an initializer, where insisting on imputing values for all the properties.
The question is: should "didSet" of the "var" fire on object creation?
Following your logic, it should! Because the variable was initialized already. So when creating the object, its value being overwritten.

Terms of Service

Privacy Policy

Cookie Policy