Edit: This question involves an assumption that was incorrect on my part. You should probably just ignore this!
This is somewhat a philosophical question, but I was just wondering if anyone would have the answer to this. Why do memberwise initializers allow you to directly access properties of structs?
I'm wondering this because of how they work with private properties.
For example, if I had:
struct Apple {
var color: String
var price: Double
}
This will create a memberwise initializer that will allow me to directly access these properties and pass in values:
let apple = Apple(color: "Red", price: 4.25)
Comparatively, I could create a similar struct with a custom initializer:
struct Pear {
var color: String
var price: Double
init(color: String, price: Double) {
self.color = color
self.price = price
}
}
This will use my custom initializer and indirectly pass in values to my properties:
let apple = Pear(color: "Red", price: 4.25)
This will really just do the same thing, except that the properties are being indirectly accessed by init() parameters. This has the added benefit of being able to mark my properties as private
, however. Because of this reason alone, I started to question why memberwise initializer don't work this way. Wouldn't it be more beneficial for memberwise initializers to indirectly access properties so I could mark the properties of Apple
as private
? What's the benefit of direct access to properties for the memberwise initializer?