Pitch: struct memberwise initialization with `let` default values

struct Struct {
   let a: X?
   let b: Y?

should autosynthesize the following initializer

init(a: X? = nil, b: Y? = nil) { ..

1 Like

This feature seems like it would be a huge foot-shotgun for most users, so I feel it's a good thing automatic defaults aren't currently provided for let-mode stored properties that don't have an attached initial expression.


This feature seems like it would be a huge foot-shotgun for most users, ..

How so? Please elaborate on your concern.

You can get what you're looking for with:

struct Test {
    private(set) var a: Int? = nil
1 Like

Cheers for the helpful response. I agree that your code meets 90% of my expectations. However, it still reads at first glances as a struct that may be mutated. In fact, extensions could change the value out from under client code. E.g.,

extension Test {
  mutating func update() {
    a = 1

As long as the extension isn't in the same file, a can't be mutated because its setter isn't accessible.

I'm glad that you agree that a can still be mutated from your example, Clay.

1 Like

Not sure what you mean? I just said that it can't be mutated under certain conditions (which are likely to be met if you're following common conventions for extensions.) But yes, if you wrote an extension in the same file with a mutating function, you could mutate a. So just don't do that?

1 Like

I apologize for hurting your feelings.

I suggest that if you're pitching this change to include some justifications as to why the change should be made. I'd recommend looking at these related threads for inspiration:


Terms of Service

Privacy Policy

Cookie Policy