[swift-evolution] Automatic generation of initializer including default values
Robert Widmann
devteam.codafi at gmail.com
Mon Oct 10 11:03:38 CDT 2016
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 at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list