Context
Consider this simple example, where we have an Actor and a Class which holds a reference to that Actor:
actor MyActor
{
func doWork() -> Int {
return 42
}
}
class MyClass
{
let worker: MyActor = MyActor()
func swift6IsJustGreat()
{
Task {
let number: Int = await worker.doWork() // ERROR: Capture of 'self' with non-sendable type 'MyClass' in a `@Sendable` closure
print(number)
}
}
}
Question
Xcode 16 Beta 1, when using Swift 6 Language Mode, shows this error on the line where we attempt to use the actor:
Capture of 'self' with non-sendable type 'MyClass' in a `@Sendable` closure
I understand the error. worker
is a property of MyClass
and the Task closure is capturing the instance of MyClass
, which is not Sendable
. Got it. But then…how the hell do I use anything in concurrency unless it’s a global?
If my actor is an iVar on a non-Sendable class, how can I ever call anything on the actor?