<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="">It's currently possible to define either or both of the following observers on a property:<div class=""><ul class=""><li class=""><i class="">willSet</i>, called just before the value is stored</li><li class=""><i class="">didSet</i>, called immediately after the new value is stored</li></ul></div><div class="">I'm finding myself using <i class="">didSet</i> extensively, but almost always guard my <i class="">didSet</i> with a clause to see if the value has changed:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class=""> class CustomView : UIView {</font></div><div class=""><font face="Menlo" class=""> var state : CustomViewState = false {</font></div><div class=""><font face="Menlo" class=""> didSet {</font></div><div class=""><font face="Menlo" class=""> guard state != oldValue else { return }</font></div><div class=""><font face="Menlo" class=""> // Act on the new state.</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class=""> }</font></div></div><div class=""><br class=""></div><div class="">Given the frequency of use, it would be great if I could strip this boilerplate altogether and simply rewrite this as:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class=""> class CustomView : UIView {</font></div><div class=""><font face="Menlo" class=""> var state : CustomViewState = false {</font></div><div class=""><font face="Menlo" class=""> didChange {</font></div><div class=""><font face="Menlo" class=""> // Act on the new state.</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class=""> }</font></div></div><div class=""><br class=""></div><div class="">Property types conforming to Equatable would be checked for the implicit guard via ==, otherwise it would fall back on the identity operator (===) for value and reference types that don't conform to Equatable.</div><div class=""><br class=""></div><div class="">This would mean the following observers could be defined on a property:</div><div class=""><div class=""><ul class=""><li class=""><i class="">willSet</i>, called just before the value is stored</li><li class=""><i class="">willChange</i>, called just before the value is stored if the value is different to the previous value</li><li class=""><i class="">didSet</i>, called immediately after the new value is stored</li><li class=""><i class="">didChange</i>, called immediately after the new value is stored if the new value is different to the previous value</li></ul></div></div><div class="">—Nathan</div></body></html>