[swift-evolution] Support for pure setters

Jessy Catterwaul mr.jessy at gmail.com
Wed Feb 3 14:21:16 CST 2016


Joe, to attempt to set foo.x doesn’t make sense. If foo cannot be gotten, a property of it will be inaccessible for getting or setting.

Set-only properties are always computed, and only used via assignment.

e.g. foo = value


> Set-only properties are problematic for Swift, because it relies on being able to do writeback in many more cases than Objective-C. A set-only property will be constrained in several surprising ways. For instance, it would be impossible to drill down to a component of a set-only value type property, because this:
> 
> var foo: Struct { get { ... } set { ... } }
> foo.x = value
> 
> is really performing this behind the scenes:
> 
> var tmp = foo // invoke getter
> tmp.x = value
> foo = tmp // invoke setter
> 
> but we would have no getter in this case to initialize the temporary with. You also wouldn't be able to use the property as an 'inout' parameter or do many of the other things you expect to be able to do with a mutable property. A set-only property is so limited I don't think it really feels like a property at all anymore. These restrictions are far more obvious if you express the interface as a function rather than a property.
> 
> -Joe
> 
> > On Jan 7, 2016, at 7:47 AM, David James via swift-evolution<swift-evolution at swift.org>wrote:
> > 
> > Currently Swift has computed properties that support get or get and set, but not set only. There are use cases where we would want set only.
> > 
> > For example, toggling a boolean which changes another stored property where it would be overkill to make a method for that. It's more intuitive to just assign a boolean. e.g. myObject.myBoolean = true
> > 
> > Another example, setting an object that is introspected in order to create a new object which is then stored on a different property. The property that is stored could be readonly/get, for example. A method for the setter (e.g. setSomething) would not be as intuitive as just a plain assignment (e.g. myObject.something = ..).
> > 
> > Another consideration is that a pure setter would support better information hiding. You may not want the parent object to expose the property. Example scenario: set a property on an object (via assignment), which creates/modifies a stored property based on the passed (set) value, and then pass the parent object to another part of the system which can than read the stored property but not the original set property — i.e. you may not want to expose the original set property to another part of the system.
> > 
> > Example:
> > 
> > var myProperty:MyClass {
> > set {
> > …
> > }
> > }
> > 
> > One concern is that without ‘get’ there really is no property at all, and perhaps this is the reason that pure setter was never included. However, this does not invalidate the above.
> > 
> > As an alternative (to make it more semantically sensible) we could introduce a new keyword ‘set’, so:
> > 
> > set myProperty:MyClass {
> > ...
> > }
> > 
> > Which would support simple assignment:
> > 
> > myObject.myProperty = myOtherObject
> > 
> > Finally, it’s important to know that this is still “computed", but only computed on the input, not on the output side.
> > 
> > David James
> > 
> > 
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> 
> 


More information about the swift-evolution mailing list