Swift struct properties with default values should be treated as optional params in default constructor

Originator:nicklockwood81
Number:rdar://19073199 Date Originated:24/11/2014
Status:Open Resolved:
Product:Developer tools Product Version:
Classification:Feature Reproducible:Always
 
Summary:
In Swift, when you define a struct, an implicit constructor is created that takes all of the properties as arguments, e.g.:

struct Foo {
  var bar: Int
  var baz: Int

//  this constructor is created implicitly
//  init(bar: Int, baz: Int) {
//    self.bar = bar
//    self.baz = baz
//  }
}

It would be useful if, when the struct properties have been declared with default values, the equivalent arguments in the default constructor would be declared as being optional, like this:

struct Foo {
  var bar: Int = 5
  var baz: Int

//  this constructor is created implicitly
//  init(bar: Int = 5, baz: Int) {
//    self.bar = bar
//    self.baz = baz
//  }
}

that way, I could construct a Foo like this:

Foo(baz: 7)

And I wouldn't have to set bar because it already has a default value

Steps to Reproduce:
See description

Expected Results:
It should not be necessary to specify properties in the constructor if they already have default values

Actual Results:
Every property must be specified in the constructor

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!