<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Mar 30, 2017, at 2:37 PM, Sean Alling via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="text-decoration: underline ; font-kerning: none" class=""><b class="">PROPOSAL:</b></span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">I propose the addition of the following new property observers applicable to all Collection types (Dictionary, Set, and Array):</span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">– <b class="">willAdd(newValue) { … }</b></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">– <b class="">didAdd(newValue) { … }</b></span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">– <b class="">willRemove(oldValue) { … }</b></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">– <b class="">didRemove(oldValue) { … }</b></span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">where, `</span><span style="text-decoration: underline ; font-kerning: none" class="">newValue</span><span style="font-kerning: none" class="">` and `</span><span style="text-decoration: underline ; font-kerning: none" class="">oldValue</span><span style="font-kerning: none" class="">` are <b class="">immutable</b>.</span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">This would allow one to perform additional work or observer logic to values added and removed from collections. This model is consistent with Swift syntax and may perhaps minimize the use of NSNotifications in some situations.</span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">Currently, in order to perform this functionality some filtering and comparison logic would have to be performed from within a `willSet { … }` and `didSet { …}` call. This change would not only ease that burden but promote a more visible and explicit expression that can further improve readability and traceability of functionality.</span></div></div></div></blockquote><div><br class=""></div>Figuring out that an arbitrary change to a collection is an "add" or a "remove" of a specific element is, well, let's just say it's complex. If you're imagining that these observers would just get magically called when someone called the add or remove method on the property, that's not really how these language features work together.</div><div><br class=""></div><div>The property behaviors proposal would let you do things like automatically computing differences and calling these observers, if you really want to do that. But the better solution is almost certainly to (1) make the collection property private(set) and (2) just declare addToList and removeFromList methods that do whatever you would want to do in the observer.</div><div><br class=""></div><div>John.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="text-decoration: underline ; font-kerning: none" class=""><b class="">EXAMPLE USAGE:</b></span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">var list = [objects]() {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>willAdd(newValue) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>… </span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>didAdd(newValue) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>…</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">}</span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">var list = [key : object]() {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>willRemove(oldValue) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>…</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>didRemove(oldValue) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>…</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">}</span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-kerning: none" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">-----</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-kerning: none" class="">Sean Alling</span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 105, 217);" class=""><span style="text-decoration: underline ; font-kerning: none" class=""><a href="mailto:allings@icloud.com" class="">allings@icloud.com</a></span></div></div>_______________________________________________<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=""></body></html>