[swift-evolution] Automatic generation of initializer including default values

Matthew Johnson matthew at anandabits.com
Mon Oct 10 15:28:08 CDT 2016


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 <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 at 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 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/20161010/ccd0182b/attachment.html>


More information about the swift-evolution mailing list