[swift-evolution] [Proposal Draft] Flexible memberwise initialization

Matthew Johnson matthew at anandabits.com
Wed Dec 23 09:32:25 CST 2015


> On Dec 22, 2015, at 1:23 PM, Félix Cloutier <felixcca at yahoo.ca> wrote:
> 
> I would like let rules to be consistent whether you have a memberwise init or not; and I would like them to be consistent with classes.
> 

The proposal recommended changing the rules everywhere so they would have still been consistent.  However, Chris was kind enough to explain in more detail why the current rules are the way they are and why they won’t be changed.  I am updating the proposal to reflect that.

> I think this places me on Guillaume's side here.
> 
> Félix
> 
>> Le 22 déc. 2015 à 14:20:47, Guillaume Lessard via swift-evolution <swift-evolution at swift.org> a écrit :
>> 
>> 
>>> On 22 déc. 2015, at 12:02, Matthew Johnson <matthew at anandabits.com> wrote:
>>> 
>>> This is not an attempt to subvert `let` properties.  The `= 1` in the declaration can very reasonably be viewed as a default value that should be used if the member is not otherwise initialized.
>> 
>> I see it as a definition.
>> 
>> 
>>> Why would you have an immutable instance member that is always going to have a constant value of 1?
>> 
>> I’m not convinced the language must prevent people from doing useless things.
>> The current model is clear. The one you suggest is much murkier.
>> (And this is a kind of situation where I would expect nifty optimizations.)
>> 
>> 
>>> That just wastes space by duplicating the constant value in many instances.  However it is quite reasonable to have an immutable instance member that defaults to 1, but may have a different value depending on the initializer that is used for the instance.
>> 
>> Shouldn’t one write an initializer with a default value, then?
>> 
>> struct A {
>> let property: Int
>> init(property: Int = 1) { self.property = property }
>> }
>> 
>> Much clearer. Using initialized `let` properties as a mere suggestion involves mental gymnastics.
>> 
>> Guillaume Lessard
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 



More information about the swift-evolution mailing list