Reputation: 535139
The following code is legal in Swift 5.5 (beta):
class Dog {
var name = "rover"
var friend : Dog? = nil
}
actor MyActor {
let dog = Dog()
}
func test() async {
let act = MyActor()
act.dog.name = "fido"
act.dog.friend = Dog()
act.dog.friend?.name = "fido"
}
Why is this legal? The dog property is shared state, isn't it? Aren't we in danger of accessing the actor's dog on different threads simultaneously? Isn't that what actors are supposed to protect us from?
Curiously, if the actor's dog
property were declared with var
instead of let
, we'd be compelled to say await
during access. Why does that make a difference? Dog is a reference type; it is mutable in place, and it is mutable in exactly the same way regardless of whether it is declared with let
or var
.
Upvotes: 9
Views: 572
Reputation:
You are right that such access is unsafe and Swift 5.5 today does not prevent this, unless you pass the -warn-concurrency
flag explicitly.
Please refer to the Staging in Sendable checking proposal (and forums post which discuss the rollout plan of the checking feature).
You can also read about the general plan with regards to concurrency safety between now in Swift 5.5 and Swift 6 in the roadmap update here: Concurrency in Swift 5 and 6.
Upvotes: 6