swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.28k stars 10.33k forks source link

Can't initialize a get-only propertyWrapper #62543

Open dabrahams opened 1 year ago

dabrahams commented 1 year ago

This should compile but doesn't (or so I claim, since there's no spec):

@propertyWrapper
struct Let<T> {
  private(set) var wrappedValue: T
  init(wrappedValue: T) { self.wrappedValue = wrappedValue }
}

struct X {
  @Let var x: Int
  init() {
    x = 5 // <=== error: cannot assign to property: 'x' is a get-only property
  }
}

swift-driver version: 1.62.15 Apple Swift version 5.7.1 (swiftlang-5.7.1.135.3 clang-1400.0.29.51) Target: arm64-apple-macosx13.0

xedin commented 1 year ago

@amritpan has a PR for let properties so I wonder whether all it would take to support this case too is to change the check from isLet to isSettable

acecilia commented 1 year ago

👋 Hi, I am experiencing this issue. After some google search I did not manage to find any other meaningful mention about this problem. Is there any swift bug or other forum discussion where I can maybe read the latest news about this? Thanks!

DreamingInBinary commented 1 year ago

@acecilia If you do this, it works:

struct X {
  @Let var x: Int
  init() {
    _x = .init(wrappedValue: 5) 
  }
}