Can we limit where property wrappers can be initialized / assigned?

I was wondering if it would make sense to introduce some kind of attribute to limit certain init or the entire type to be manually constructible from the context of another init.

The main example that I have in mind is SwiftUI.State. At this moment there's no way to prevent the users from initializing the PW from an init of their view types and passing down some non constant initial value. This works, but it has some interesting side effects as the initial value will be replaced by the framework through user transparent injection during the lifetime of that view. This is a common pitfall for many new developers starting with SwiftUI.

I'm not sure if there are even other use cases for this idea, feel free to extend the motivation for this.

Here's an example on what I have in mind:

@propertyWrapper
struct PW {
  @_our_new_attribute
  init(wrappedValue: Int) { ... }
}

struct Foo {
  @PW
  var a: Int = 42 // okay

  @PW
  var b: Int
 
  init(c: Int) {
    self.b = c // new error
  }

  init(d: Int) {
    self._b = PW(wrappedValue: d) // new error
  }

  init() {
    // new error as well - no mercy to workarounds
    // through escaping
    self._b = { 
      PW(wrappedValue: 3)
    }() 
  }
}

Especially the last init shows that it would be tricky to limit the initialization on its own. Therefore the new attribute could potentially "just" limit that the the non-synthesized / user generated assignment of the PW property would be rejected from within an init. In that situation the attribute would actually need to applied on the whole PW type I believe.


Extending types like State in SwiftUI will break some existing code bases, so if something like this ever emerges it should introduce a warning in Swift 5.x.y but become an error in Swift 6.


Thoughts?