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

Charlie Monroe charlie at charliemonroe.net
Sat Aug 27 01:47:16 CDT 2016


The newValue is already set in the property, so you just use that property. I don't see much benefit in having "newValue" instead of just accessing it:

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

And the same goes for willSet, where you have newValue, but not "oldValue", since oldValue is still in the stored property...

> On Aug 26, 2016, at 8:20 PM, Eric Miller via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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)
> }}
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list