[swift-evolution] Proposal: Local identifier for the newValue in didSet and willSet blocks

Eric Miller hi at simple.gy
Fri Aug 26 13:20:02 CDT 2016


Just an idea.

I use didSet a lot, and it'd be nice to have a generic way to access the
newValue.

For UI modes:

var anonymousUserMode = false { didSet {
  if oldValue == anonymousUserMode { return }
  renderLoginOverlay(show: anonymousUserMode)
}}

For animated display strings:

var errorMessage: String? = nil { didSet {
  if oldValue == errorMessage { return }
  errorLabel.text = errorMessage
  // Do some sliding or hiding based on the new value
}}

Has the idea of using $0 / $1 or oldValue / newValue been considered?

I'd like to be able to write this as:

var anonymousUserMode = false { didSet {
  if oldValue == newValue { return }
  renderLoginOverlay(show: newValue)
}}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160826/5b0487ce/attachment.html>


More information about the swift-evolution mailing list