Search code examples
swiftgetter-setterswift-protocols

Why am I allowed to set a read only property of a protocol using a struct that inherits said protocol?


I'm following a tutorial on the protocol oriented programming paradigm in which I'm confused by something I thought was rather simple which is read only properties of protocols or getters and setters. My understanding is that a read only property is signified by using the keyword 'get' when declaring a variable within a protocol. I was excited so I quickly coded created a playground to see if my thinking was accurate however it appears that I can still change the property which I thought was read only. What am I doing wrong to make it a true read only property to where I can't set it?

protocol FullName {
    var firstName: String {get set}
    var lastName: String {get set}
    var readOnlyProperty: String {get}

}

struct OuttaBeerOuttaHere: FullName {
    var firstName: String

    var lastName: String

    var readOnlyProperty: String = "Jack! Jack!...Line from Titanic"

}

var leonardoDicaprio = OuttaBeerOuttaHere.init(firstName: "Leonardo", lastName: "Dicaprio", readOnlyProperty: "WTF")

print(leonardoDicaprio.readOnlyProperty) //prints "WTF"

leonardoDicaprio.readOnlyProperty = "what now"

print(leonardoDicaprio.readOnlyProperty) //prints "what now"

Solution

  • What am I doing wrong to make it a true read only property to where I can't set it?

    There is a difference between a protocol (a set of rules) and the type (i.e. your struct) that adopts the protocol.

    • Your protocol rule says that readOnlyProperty should be readable.

    • Your struct obeys by making it readable, and also makes it writable. That is not illegal, so all is well — and readOnlyProperty in your struct is read-write.

    What would have been illegal would be the inverse, i.e. for the protocol to declare a property read-write but the adopter to declare it read-only. That situation didn't arise in your example, but if it had, the compiler would have stopped you.