[swift-evolution] [Proposal Draft] Flexible memberwise initialization
Cameron Knight
camjknight at mac.com
Tue Dec 22 13:16:46 CST 2015
Properties with initial values
struct S {
let s: String = "hello"
let i: Int = 42
// user declares:
memberwise init() {}
// compiler synthesizes:
init(s: String = "hello", i: Int = 42) {
self.s = s
self.i = i
}
}
I think the sticking issue here is that this synthesis isn't congruent with the current rules around let:
struct S {
let s: String = "hello"
let i: Int = 42
init(s: String = "hello", i: Int = 42) {
self.s = s
self.i = i
//note: initial value already provided in 'let' declaration
//note: change 'let' to 'var' to make it mutable
}
}
4) var properties with default initializers should have their parameter to the synthesized initializer defaulted.
I don't think anyone would have issue with this:
struct S {
var s: String = "hello"
var i: Int = 42
// user declares:
memberwise init() {}
// compiler synthesizes:
init(s: String = "hello", i: Int = 42) {
self.s = s
self.i = i
}
}
> On Dec 22, 2015, at 1:02 PM, Matthew Johnson via swift-evolution <swift-evolution at swift.org> wrote:
>
>
>> On Dec 22, 2015, at 12:51 PM, Guillaume Lessard via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> Hi,
>>
>> Regardless of anything else in the proposal, I strongly dislike its attempted subversion of `let` properties.
>>
>> struct A {
>> let member = 1 // It’s done, over. If that’s not what you want, don’t initialize it here.
>> }
>>
>
> 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.
>
> Why would you have an immutable instance member that is always going to have a constant value of 1? 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.
>
> Matthew
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151222/45531bb9/attachment.html>
More information about the swift-evolution
mailing list