[swift-evolution] Ability to set access control independently for getter & setter of a property

Evan Maloney emaloney at gilt.com
Fri Dec 4 13:48:16 CST 2015


Currently, Swift supports a single level of access controls for properties. If a read/write property is declared as public, both the getter and the setter of that property will be publicly visible.

However, it is often the case that you need a stored property that is publicly read-only but internally or privately writeable. The current way to achieve this is by doing something like:

    public var authenticationValidUntil: NSDate? {
        return _authenticationValidUntil
    }
    private var _authenticationValidUntil: NSDate?

This seems like a lot of boilerplate to achieve a common goal. Objective-C supported a similar concept by being able to declare a property as read-only in a .h file while being able to override this as a read-write property in the .m file.

I propose being able to declare separate access controls for a property's getter and setter, wherein the setter could have more restrictive access controls than the getter.

For example:

    var authenticationValidUntil: NSDate? { public get, private set }

This would result in a public getter, while the implementer could still set the value of the property.

Curious to get your thoughts on this.

Regards,

E. Maloney
Gilt Groupe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/c23555c7/attachment.html>


More information about the swift-evolution mailing list