[swift-evolution] [Proposal Draft] Flexible memberwise initialization
matthew at anandabits.com
Tue Dec 22 11:30:16 CST 2015
> On Dec 22, 2015, at 11:22 AM, Thorsten Seitz <tseitz42 at icloud.com> wrote:
>> Am 22.12.2015 um 05:43 schrieb Matthew Johnson via swift-evolution <swift-evolution at swift.org>:
>> What do you think the downsides are of synthesizing memberwise initialization for properties with an “initial value”?
> When using the memberwise initializer those properties are always assigned a value twice. First the initial value and then the value provided by the initializer. The first value is constructed and assigned unnecessarily and in corner cases this might even be problematical, e.g. if creating a value of a certain kind has a side effect like incrementing an instance counter or logging something.
My proposal is specifically suggesting that we treat “initial value” as a default rather than an initialization that always happens. IMO the current behavior is limiting and problematic in a number of ways.
If we make the change I am suggesting double initialization / assignment will not happen.
Maybe I should have stated the question more clearly:
What do you think the downsides are of allowing synthesized memberwise initialization to supercede an “initial / default value”?
If initializers cannot initialize the member to something other than the “initial value” then “initial values” are largely useless for immutable members. Every instance gets the same value, thus wasting space. Of course there are exceptions to this such as computed initializers, reference types, etc. where this isn’t exactly true, but in a large number of cases what is really desired is a default value that is used when the initializer doesn’t initialize the member with a different value.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution