[swift-evolution] stored properties in extensions (was: associated objects)
Charlie Monroe
charlie at charliemonroe.net
Mon Oct 10 14:47:46 CDT 2016
Yes, there are valid use cases.
For example, in case of many UI elements, you do not have control over their instantiation and need to associate a "help description" with them as an example. Which can be done by an additional property helpDescription added to NSView/UIView.
Another example that I've used is with CoreData, where you can store additional information on the MOC - NSManagedObjectContext is strongly discouraged from being subclassed. Example of the additional information is a synchronization manager assigned to the MOC.
And since in Swift, classes can be final, there may not be a way to subclass the class to add your properties instead.
IMHO, it is a valid request to be able to add additional stored properties via extensions.
> On Oct 10, 2016, at 9:15 PM, Charles Srstka <cocoadev at charlessoft.com> wrote:
>
> Right. The question is whether we *need* to add stored properties out-of-module, and what the use case for that is. To me it seems that adding them in-module is by far the more common use case, for the purposes of implementing protocols.
>
> At any rate, the rewrite option would be a great addition to Swift regardless of what our answer to the first question is.
>
> Charles
>
>> On Oct 9, 2016, at 4:32 PM, Jay Abbott <jay at abbott.me.uk <mailto:jay at abbott.me.uk>> wrote:
>>
>> Charles,
>>
>> That would be good. It is a nicer way to write what is already possible to achieve, but it's not a way to 'implement' stored properties in extensions.
>>
>> On Sun, 9 Oct 2016 at 21:45 Charles Srstka <cocoadev at charlessoft.com <mailto:cocoadev at charlessoft.com>> wrote:
>> *Replace both instances of “class C: P” with just “class C” since the conformance comes in the extension. That’s what I get for writing this quickly.
>>
>> Charles
>>
>>> On Oct 9, 2016, at 3:43 PM, Charles Srstka <cocoadev at charlessoft.com <mailto:cocoadev at charlessoft.com>> wrote:
>>>
>>> protocol P {
>>> var foo: String { get }
>>> func bar()
>>> }
>>>
>>> protocol Q {
>>> var baz: Int { get }
>>> func qux()
>>> }
>>>
>>> class C: P {
>>> var foo: String // <- what is this doing here?
>>> var baz: Int // <- ditto
>>> }
>>>
>>> extension C: P {
>>> func bar() {}
>>> }
>>>
>>> extension C: Q {
>>> func qux() {}
>>> }
>>>
>>> we could simply:
>>>
>>> protocol P {
>>> var foo: String { get }
>>> func bar()
>>> }
>>>
>>> class C: P {}
>>>
>>> extension C: P {
>>> var foo: String
>>> func bar() {}
>>> }
>>>
>>> extension C: Q {
>>> var baz: Int
>>> func qux() {}
>>> }
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161010/4e140594/attachment.html>
More information about the swift-evolution
mailing list