[swift-evolution] Support for pure setters --> private(get)
David James
davidbjames1 at gmail.com
Fri Jan 8 10:17:55 CST 2016
After considering this further, I think the answer is simpler and more in line with design goals.
Proposal:
• Support set-only real or computed properties limited by access level.
• To achieve this, add private(get) / internal(get) since we already support private/internal(set)
• Make get computed property optional (with set)
This would support patterns where client code can set data on file/module based structures, allowing that data to be read within the file/module structure. This supports information hiding where the client should only push data and allow modules to respond to that. Other parts of the code should not be reading this data and making assumptions. It should be hidden.
One example design pattern is the Builder pattern. The client could set various properties, and the builder would aggregate these and return a configured object.
This also provides a cleaner style of setting and more consistent style of setting, foo.prop = bar, rather than using function syntax, foo.setProp(bar), for this use case (where we want set-only), while using dot syntax for other use cases (where we want get and set).
Examples:
private(get) var foo:Foo
With computed setter,
private(get) var foo:Foo {
get {
return self.foo
}
set {
self.foo = newValue
self.bar = Bar(foo: self.foo) // e.g. Builder pattern
}
}
or just make the computed getter optional,
private(get) var foo:Foo {
set {
self.foo = newValue
self.bar = Bar(foo: self.foo)
}
}
Is there a compelling argument not to support this? (aside from, "why bother, you can use a function setter")
David
> On Jan 7, 2016, at 8:50 PM, Wallacy <wallacyf at gmail.com> wrote:
>
>
> "The downside of using a method is it’s just not as convenient/elegant to use than simple assignment."
>
> "pure setter" is also pretty confusing. I can set but not read the value again?
>
> If you want a "simpler" setter, you can define a closure or unlabeled func.
>
> class iClass{
> let myProperty:(String)->() = { value in
> print("1: \(value)");
> }
> func myProperty(value: String)->() {
> print("2: \(value)");
> }
> func otherProperty(value: String)->() {
> print("3: \(value)");
> }
> }
>
> iClass().myProperty("a");
> //iClass().myProperty(value: "b"); // How to call the function in this case?
> iClass().otherProperty("c");
>
> It's more clear than:
>
> iClass().myProperty == "a";
> iClass().otherProperty == "c";
>
> FWIW:
>
> func "myProperty(value: String)->()" seems to be hidden by closure "myProperty:(String)->()".
>
> Em qui, 7 de jan de 2016 às 15:32, David James via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> escreveu:
> Perhaps “pure setters” is a misnomer. Probably more accurate would be to call it a “computed setter”?
>
> To your point about properties being for lookup, understandable. That’s why I suggested introducing the “set” keyword instead of using var .. set.
>
> The downside of using a method is it’s just not as convenient/elegant to use than simple assignment.
>
> David
>
>> On Jan 7, 2016, at 6:16 PM, Félix Cloutier <felixcca at yahoo.ca <mailto:felixcca at yahoo.ca>> wrote:
>>
>> .NET has them and I never felt that I needed them. For me, a property implies something that can be looked up.
>>
>> What's the downside of using a method?
>>
>> Félix
>>
>>> Le 7 janv. 2016 à 10:47:20, David James via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> a écrit :
>>>
>>> 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
>
>
> _______________________________________________
> 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/24a0bc0e/attachment.html>
More information about the swift-evolution
mailing list