Guy_Miller
(Guy Miller)
1
Hi,
When I am defining a struct I find myself often using a style as shown in the following example:
struct Foo {
var greeting: String
var x: Int
var y: Int
var z: Int
init(greeting: String, x: Int = 1, y: Int = 2, z: Int = 3) {
self.greeting = greeting
self.x = x
self.y = y
self.z = z
}
}
This enables one to write code when one doesn’t need to change defaults:
let f = Foo(greeting: “Hello”)
and when one wishes to change one of the defaults:
let f = Foo(greeting: “Hello”, z: 4)
It would be better if one could write the struct in what I understand is the preferred style:
struct Foo {
var greeting: String
var x = 1
var y = 2
var z = 3
}
and have the compiler generate the initializer:
init(name: String, x: Int = 1, y: Int = 2, z: Int = 3) {
self.name = name
self.x = x
self.y = y
self.z = z
}
rather than one where all the parameters that have a default value need to be specified in the initializer if one wishes to change just one of them.
Regards,
Guy Miller
codafi
(Robert Widmann)
2
I'm ambivalent here. I know this is convenient, but I have a stronger interest in controlling my APIs, initializers included. The more the compiler synthesizes, the less control I have and the more code I have to write to make up for that. I don't think there's enough here (yet?) to justify synthesis.
tl;dr Not everybody chains convenience initializers like this.
~Robert Widmann
2016/10/10 8:27、Guy Miller via swift-evolution <swift-evolution@swift.org> のメッセージ:
···
Hi,
When I am defining a struct I find myself often using a style as shown in the following example:
struct Foo {
var greeting: String
var x: Int
var y: Int
var z: Int
init(greeting: String, x: Int = 1, y: Int = 2, z: Int = 3) {
self.greeting = greeting
self.x = x
self.y = y
self.z = z
}
}
This enables one to write code when one doesn’t need to change defaults:
let f = Foo(greeting: “Hello”)
and when one wishes to change one of the defaults:
let f = Foo(greeting: “Hello”, z: 4)
It would be better if one could write the struct in what I understand is the preferred style:
struct Foo {
var greeting: String
var x = 1
var y = 2
var z = 3
}
and have the compiler generate the initializer:
init(name: String, x: Int = 1, y: Int = 2, z: Int = 3) {
self.name = name
self.x = x
self.y = y
self.z = z
}
rather than one where all the parameters that have a default value need to be specified in the initializer if one wishes to change just one of them.
Regards,
Guy Miller
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
anandabits
(Matthew Johnson)
3
We considered a proposal I wrote early in the year. There was extensive discussion of the topic at that time. The core team decided to defer the topic at the time. Everyone was convinced there are better ways to handle it than I originally proposed (including myself). The topic will be revisited at some point in the future but is not in scope for Swift 4, phase 1.
If you wish to catch up on the discussion, here’s a link to the proposal which includes links to the discussion as well as the core team’s rationale for their decision on this proposal: https://github.com/apple/swift-evolution/blob/master/proposals/0018-flexible-memberwise-initialization.md
···
On Oct 10, 2016, at 7:27 AM, Guy Miller via swift-evolution <swift-evolution@swift.org> wrote:
Hi,
When I am defining a struct I find myself often using a style as shown in the following example:
struct Foo {
var greeting: String
var x: Int
var y: Int
var z: Int
init(greeting: String, x: Int = 1, y: Int = 2, z: Int = 3) {
self.greeting = greeting
self.x = x
self.y = y
self.z = z
}
}
This enables one to write code when one doesn’t need to change defaults:
let f = Foo(greeting: “Hello”)
and when one wishes to change one of the defaults:
let f = Foo(greeting: “Hello”, z: 4)
It would be better if one could write the struct in what I understand is the preferred style:
struct Foo {
var greeting: String
var x = 1
var y = 2
var z = 3
}
and have the compiler generate the initializer:
init(name: String, x: Int = 1, y: Int = 2, z: Int = 3) {
self.name = name
self.x = x
self.y = y
self.z = z
}
rather than one where all the parameters that have a default value need to be specified in the initializer if one wishes to change just one of them.
Regards,
Guy Miller
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution