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

Guillaume Lessard glessard at tffenterprises.com
Tue Dec 22 13:20:47 CST 2015


> 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



More information about the swift-evolution mailing list