-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-10203] Subscripts setter on key-path calls getter of the destination property #52603
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
Comments
Running into this bug as well. |
Same, this bug is unfortunately preventing me from implementing a useful API that uses key paths. Would love to see a fix for it! |
A slightly simpler example: struct Foo {
private var isInitialized: Bool = false
var value: Int {
get {
guard isInitialized else {
fatalError("property accessed before being initialized")
}
return 1
}
set {}
}
}
do {
var x = Foo()
x[keyPath: \.value] = 0
} |
This issue still happens and is also affecting an API that we were trying to build. Specifically, the getter runs an expensive operation to get the value so we want to avoid doing that unnecessarily. struct Foo {
var value: Int {
get { fatalError("Should not call get") }
set {}
}
}
var foo = Foo()
foo[keyPath: \.value] = 0 Confusingly, this is exactly how SwiftUI |
Having the same problem! @gabriellanata SwiftUI probably does not run into this problem because environment variables always have a value (the default value, if it has not been overwritten). So, they don't have to use |
Because of a Swift bug swiftlang/swift#52603
I'm running into this issue as well... has anyone found a workaround? |
Because of a Swift bug swiftlang/swift#52603
Same issue with swift 6 struct Foo {
var value: Int {
get { fatalError("Should not call get") }
set { }
}
mutating func with<Value>(_ keyPath: WritableKeyPath<Self, Value>, _ value: Value) {
self[keyPath: keyPath] = value
}
}
var foo = Foo()
foo[keyPath: \.value] = 1 // work;s fine
foo.with(\.value, 2) // Error |
Environment
Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)
Target: x86_64-apple-darwin18.2.0
Additional Detail from JIRA
md5: 13afefaa36d850c75676dbe3a222ba64
Issue Description:
This example unexpectedly crashes:
Discussion on the forums: https://forums.swift.org/t/keypaths-subscript-setter-accesses-the-properties-getter/22212
The text was updated successfully, but these errors were encountered: