[swift-evolution] Support for pure setters

Joe Groff jgroff at apple.com
Fri Jan 8 12:00:56 CST 2016


> On Jan 8, 2016, at 9:23 AM, David James <davidbjames1 at gmail.com> wrote:
> 
> Is this true even if there is a getter that is private? e.g. private(get) (see my latest reply before this one)

Yeah. If you can't see the getter, then the property is effectively set-only to you and has all the same problems.

-Joe

>> On Jan 8, 2016, at 5:51 PM, Joe Groff <jgroff at apple.com <mailto:jgroff at apple.com>> wrote:
>> 
>> 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 <mailto: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 <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
> 
> David James

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


More information about the swift-evolution mailing list