[swift-users] Should Array's `append(_)` function cause `didSet`?

Jordan Rose jordan_rose at apple.com
Mon Jul 10 13:29:02 CDT 2017



> On Jul 7, 2017, at 22:50, Marco S Hyman via swift-users <swift-users at swift.org> wrote:
> 
> 
>> On Jul 7, 2017, at 9:48 PM, Zhao Xin <owenzx at gmail.com> wrote:
>> 
>> Thank you very much Marco. But What is  “outside of an initializer” really bothers me. **Both** `func bar(keysAndValues:Dictionary<String, String>)` works now. **Are they really outside ?**
> 
> Uhhh, that is certainly not the results I’d have expected.  Perhaps one of the  swift language lawyers can explain.

The goal is that once the initializer is completed all accesses will go through the setter and therefore trigger willSet/didSet behavior. Since a local function can be assigned to a property or something and get called later, it has to go through the setter as well. So the rules only apply to what's directly in the body of the initializer, not anything nested. (This includes closures, even.) It might be worth a bug against us at Apple to make this more explicit in the documentation, https://bugreport.apple.com <https://bugreport.apple.com/>.

We also have a bug where 'defer' can trigger willSet and didSet behavior as well, SR-1437 <https://bugs.swift.org/browse/SR-1437>. But that really is just a bug.

Jordan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170710/028bfe6d/attachment.html>


More information about the swift-users mailing list