Last active
December 15, 2022 13:00
-
-
Save inamiy/808047a83cd9dfe644828342350da1e5 to your computer and use it in GitHub Desktop.
`Task { [weak self] in self?.prop = value }` causes non-isolated mutation error https://twitter.com/inamiy/status/1603267958209331200
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
actor A { | |
var prop: Int = 0 | |
func foo() { | |
// OK | |
Task { | |
self.prop = 1 | |
} | |
// ERROR: Actor-isolated property 'prop' can not be mutated from a non-isolated context | |
Task { [weak self] in | |
self?.prop = 1 | |
} | |
// ERROR: Actor-isolated property 'prop' can not be mutated from a non-isolated context | |
Task { [weak self] in | |
guard let self = self else { return } | |
self.prop = 1 | |
} | |
// Requires setter and `await` | |
Task { [weak self] in | |
await self?.setProp(1) | |
} | |
} | |
func setProp(_ prop: Int) { | |
self.prop = prop | |
} | |
} | |
let a = A() | |
await a.foo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Raised ticket: swiftlang/swift#62604