[swift-evolution] Proposal: Allow willChange and didChange observers on a property

Chris Lattner clattner at apple.com
Sun Dec 6 10:42:45 CST 2015


Yes, it that proposal is capable of expressing this, then that is how I would prefer property observers in general to work.

-Chris

> On Dec 6, 2015, at 3:27 AM, David Hart <david at hartbit.com> wrote:
> 
> Chris, wouldn't that be better designed/implemented with the property delegate functionality Joe Groff is working on?
> 
>> On 06 Dec 2015, at 07:52, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> 
>>> On Dec 5, 2015, at 10:42 PM, Mosab Elagha <elagha2 at illinois.edu> wrote:
>>> 
>>> +1. This feature would allow for a more clean approach in checking the existing state of a value. 
>>> 
>>> > Also, can a property have these observers as well as the existing ones?
>>> 
>>> I'm not really familiar with the implementation of the observers, but why wouldn't it be able to? Maybe didSet can precede didChange in order of execution if both are implemented, so you can have default behavior when accessing a value but certain other behavior if it has changed. Or maybe have willChange/didChange be a property that can be accessed within willSet/didSet (like oldValue).
>> 
>> I believe it is possible to implement, I’m just trying to give guidance on the sorts of things a formal proposal should address.
>> 
>> -Chris
>> 
>>> 
>>> 
>>> 
>>> -Mosab Elagha
>>> 
>>>> On Sun, Dec 6, 2015 at 12:11 AM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>>>> 
>>>>> On Dec 5, 2015, at 4:00 PM, Nathan de Vries via swift-evolution <swift-evolution at swift.org> wrote:
>>>>> 
>>>>> It's currently possible to define either or both of the following observers on a property:
>>>>> willSet, called just before the value is stored
>>>>> didSet, called immediately after the new value is stored
>>>> 
>>>> ...
>>>> 
>>>>> This would mean the following observers could be defined on a property:
>>>>> willSet, called just before the value is stored
>>>>> willChange, called just before the value is stored if the value is different to the previous value
>>>>> didSet, called immediately after the new value is stored
>>>>> didChange, called immediately after the new value is stored if the new value is different to the previous value
>>>> 
>>>> I’m open to consider a change along these lines, but the proposal would have to be fleshed out more.  Presumably it would only work for equatable types, right?  Also, can a property have these observers as well as the existing ones?  Would it be possible to add *one of* didChange or willChange - since less is better?
>>>> 
>>>> -Chris
>>>> 
>>>> 
>>>> _______________________________________________
>>>> swift-evolution mailing list
>>>> swift-evolution at swift.org
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151206/fbde7bda/attachment.html>


More information about the swift-evolution mailing list