<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">You should check out my proposal for property observers. You should be able to implement observation as a behavior with that feature.<div class=""><br class=""></div><div class="">-Joe</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 22, 2015, at 3:16 AM, Mohamed Ebrahim Afifi via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class=""><div class="">While willSet and didSet is a great property observers. They do work only on the declaration of the property. But for other objects to observe this, then they need to use KVO which is an Objective-C mechanism (We use String to subscribe, it doesn't offer generic capabilities for change). Besides, it's not portable.</div><div class=""><br class=""></div><div class=""><div class="">// A native Swift class or struct</div><div class="">class Foo {</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; // property with any type</div><div class="">&nbsp; &nbsp; // We might have a keyword for declaring the property observable</div><div class="">&nbsp; &nbsp; /*@obserable*/ var bar: Int = 0</div><div class="">}</div><div class=""><br class=""></div><div class="">let instance = Foo()</div><div class=""><br class=""></div><div class="">// anyone can subscribe to changes of the `bar` property.</div><div class="">instance.bar += { newValue in</div><div class="">&nbsp; &nbsp; print(newValue)</div><div class="">}</div><div class=""><br class=""></div><div class="">// you can store the return value to unsubscribe later.</div><div class="">let observer = instance.bar += { newValue in</div><div class="">&nbsp; &nbsp; print(newValue)</div><div class="">}</div><div class=""><br class=""></div><div class="">// unsubscribe</div><div class="">instance.bar -= observer</div><div class=""><br class=""></div><div class="">// Also we can have a protocol to be implemented by any class/struct to give special events. For example Array would implement events for Insertion, Deletion, etc. Same for Dictionary and developers can do the same for their own classes.</div></div></div><div class=""><br class=""></div><div class="">You can have a look at implementation for a similar library here&nbsp;<a href="https://github.com/slazyk/Observable-Swift" class="">https://github.com/slazyk/Observable-Swift</a></div><div class="">But as you can see there some issues like accessing the underlying value needs an operator which I believe with a native observer we will not have this issue.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class="">Best Regards,<div class="">Mohamed Afifi</div></div></div></div></div></div>
</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=RoDF4MveSEMYBIqIJA6ub1g8cOZ-2BVYvqV-2FqygPhjPn8isNJunfIQntkZxaAW-2Fl9tfAsYFbx-2Boee8s-2FqPSqq8AbeJa-2FWxXjbGv-2FXQEOjeWqxjdZKjBxZLOTHT-2BIL9IqLRHOQYpJ5d4tj-2FPp-2FpMdzTkatJT08CjPt9x43lFGspKxnh6Ms5bJqs27cdhWG054kIvH9AEE0F30w2i-2BnyHEgyOnJwBcWrfwJtTkUwPaxhfKs-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>