Search code examples
swiftclassstructaccess-control

Why you not use an open keyword with structs in swift?


everywhere talk about only classes with open/public keyword so it would be great if swift experts can provide satisfying answers with an example.

for eg: we can do this in class but don't do it in the struct!

open class Animal {
    var name: String
    var age: Int
    
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}

Solution

  • open is irrelevant to structs, since they don't have inheritance. public relates to visibility from outside the module, so it is relevant to all types.