Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-12178] willSet not firing when used with a property wrapper #54603

Closed
swift-ci opened this issue Feb 11, 2020 · 3 comments
Closed

[SR-12178] willSet not firing when used with a property wrapper #54603

swift-ci opened this issue Feb 11, 2020 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself property wrappers Feature: property wrappers

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12178
Radar rdar://problem/59496047
Original Reporter camcaine (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Swift 5.2

Xcode Version 11.4 beta (11N111s)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, PropertyWrappers
Assignee @theblixguy
Priority Medium

md5: 7ffc4169185cec192e0c089a29182fa6

relates to:

  • SR-12089 Swift 5.2 snapshot: didSet not called

Issue Description:

willSet observer doesn't fire when used alongside a property wrapper:

@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)
@theblixguy
Copy link
Collaborator

Simpler reproducer that doesn't involve Apple APIs like Measurement, etc:

@propertyWrapper 
struct Foo {
  private var _storage: [Int] = []

  init(wrappedValue value: [Int]) {
    self._storage = value
  }

  var wrappedValue: [Int] {
    get { _storage }
    set { _storage = newValue }
  }
}

class Bar {
  @Foo var someArray = [1, 2, 3] {
    willSet {
      print(newValue) 
    }
  }
}

let bar = Bar()
bar.someArray[0] = 4 // Doesn't call willSet

@beccadax
Copy link
Contributor

@swift-ci create

@slavapestov
Copy link
Member

#29931

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself property wrappers Feature: property wrappers
Projects
None yet
Development

No branches or pull requests

4 participants