I think you misunderstand this proposal. It would make no change and have no effect on the code example you posted above.
let optional: Int?
has never received default nil initialization because if they did, you couldn't change their value because they're constants. @sveinhal is correct in saying that you wouldn't receive a warning whether optional
was a let
or a var
.
struct Thing {
let field: Int
let optional: Int? // no warning here
var optional2: Int? // still no warning here
}
You would still receive the same memberwise initializer where you would initialize both optionals.
I mean that let
already work in accordance to this proposal. This only proposes to make var
work the same with regards to initialization.
The gain would be correctness and help from the compiler to avoid subtle and hard to find bugs. It would have no effect on the synthesized memberwise init, the Decodable
init or anything like that.
But it would make it an error (or warning) to not initialize optional vars in your own inits.
Sorry, I've never noticed that difference before.
It is unclear to me what the going forward process is for these types of enhancements. It would seem to me that the core team needs to decide how to handle these kind of improvements that could potentially have a very long deprecation period.
+1
+/- 0.
Why can't we just compromise and have a compiler flag to enable a warning? Does it have to be one way or the other?
+1 Letâs push for the flag.
Yes, it does. It is an intentional design choice that there should not be âdialectsâ of Swift (with the exception of older language versions and library resiliency mode, unavoidable for obvious reasons).
Therefore, with the exception of language mode and resiliency, Swift flags do not change what does and doesnât compile, and Swift doesnât have optional warnings.