The structs generated initializer should translate default member values into default parameters.
| Originator: | rosko21 | ||
| Number: | rdar://22258151 | Date Originated: | 12-Aug-2015 03:33 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
There are many instances where I find myself creating structs that are simply configuration objects for an API (such as `attributes` for `NSAttributedString`). Since only a subset of the configurations provided by the object need to be customized or provided at any one time, I generally write an `init` that takes all of the properties, but provides default values. This allows the user to only configure what they care about, i.e.:
```
struct Options {
let foo: String?
let bar: Bool
let baz: Int
init(foo: String? = nil, bar: Bool = true, baz: Int = 0) {
self.foo = foo
self.bar = bar
self.baz = baz
}
}
let options = Options(bar: false)
```
While I find this to be an incredibly powerful tool, there’s a whole lot of boiler plate going on, and `Options` only has 3 properties. It would be amazing if Swift could generate this init for me by inferring the default values in the initializer from the default values of the properties itself.
Steps to Reproduce:
Attempt to compile the following:
```
struct Options {
let foo: String? = nil
let bar: Bool = true
let baz: Int = 0
}
let options = Options(bar: false)
```
Expected Results:
This compiles, because the generated initializer can take `bar` and `bad` as optional parameters with default values.
Actual Results:
This does not compile, because the generated initializer is simply `Foo()`
Regression:
N/A
Notes:
N/A
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!