[swift-evolution] [Pitch] Collection Type Element Property Observers

Sean Alling allings at icloud.com
Thu Mar 30 13:37:37 CDT 2017


PROPOSAL:

I propose the addition of the following new property observers applicable to all Collection types (Dictionary, Set, and Array):

– willAdd(newValue) { … }
– didAdd(newValue) { … }

– willRemove(oldValue) { … }
– didRemove(oldValue) { … }

where, `newValue` and `oldValue` are immutable.

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.

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.


EXAMPLE USAGE:

var list = [objects]() {
	willAdd(newValue) {
		… 
	}
	didAdd(newValue) {
		…
	}
}

var list = [key : object]() {
	willRemove(oldValue) {
		…
	}
	didRemove(oldValue) {
		…
	}
}


-----
Sean Alling
allings at icloud.com <mailto:allings at icloud.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170330/36db72ce/attachment.html>


More information about the swift-evolution mailing list