Property wrapper observer not firing

I have a willSet observer that doesn't fire when using to a property wrapper. I was under the impression it should work. Am I missing something? (running on Swift 5.2, Xcode 11.4-beta)
Thanks in advance!

@propertyWrapper struct RoundedMeasurement {
    private var measurement: Measurement<UnitMass>

    init(wrappedValue: Measurement<UnitMass>) {
        measurement = wrappedValue.rounded()
    }

    var wrappedValue: Measurement<UnitMass> {
        get { measurement }
        set { measurement = newValue.rounded() }
    }
}

class Weighted {
    @RoundedMeasurement
    var weight = Measurement<UnitMass>(value: 10, unit: .kilograms) {
        willSet {
            print("New Value \(newValue.description)")
        }
    }
}

let weighted = Weighted()
// willSet only fires when the property wrapper is not used.
weighted.weight.convert(to: .pounds)
1 Like

Looks like a regression from Xcode 11.3
You can file a bug report on bugs.swift.org or https://feedbackassistant.apple.com/

1 Like

I can also confirm the regression, please file a bug at: bugs.swift.org

Meanwhile you can workaround it by manually implementing get & set.

get {
  _weight.wrappedValue
}
set {
  // FIXME: Move to `willSet` when bug SR-XXXX is resolved
  do {
     print("New Value \(newValue.description)")
  }  
  _weight.wrappedValue = newValue
}
2 Likes

Looks related to https://bugs.swift.org/browse/SR-12089

1 Like

I think what's happening here is that the call to .convert ends up calling the _modify accessor which is somehow interfering with the observers (it seems like the accessor calls wrappedValue's setter after resuming).

Terms of Service

Privacy Policy

Cookie Policy