Access level of Struct memberwise initializer

The default memberwise initializer for a structure type is considered private if any of the structure’s stored properties are private. why?

struct People {
    private var name: String
    var height: String
    init(name: String, height: String) {
        self.name = name
        self.height = height
    }
}
let p = People(name: "see", height: 12) //right
struct People {
    private var name: String
    var height: String
}

let p = People(name: "see", height: 12) //error
1 Like

If a stored property is private, it's assumed that that field isn't one that other files should know about. That means that the memberwise initializer can't advertise its existence.

1 Like

That said, the deferred memberwise init proposal includes the ability to declare the initializer and specify the access level explicitly while still having a synthesize implementation. I'm looking forward to revisiting that if / when somebody is willing to work on a prototype implementation (I don't have bandwidth to do it myself).

2 Likes