Something I find myself often wanting to do is provide a guarantee that a non-Sendable type is isolated to some actor, but it doesn't actually matter which. Today, this is very easily and conveniently achieved by just slapping a global actor on the type. Defining a whole new global actor rarely feels right, especially for a library so I just sigh, use MainActor, and move on.
@MainActor // <- I don't want to do this but all other options feel worse
class MyClass {
private var internalState = 0
func doSomeStuff() {
// must be on creating actor here, because this is a non-Sendable type
Task {
let value = await someAsyncFunction()
// now can capture self safely here
self.internalState += value
}
}
}
I've been reading the proposal for inheriting a caller's actor isolation, and it seems really powerful. It seems similar in spirit, but doesn't cover my issue (I think).
Would this proposal handle this scenario? I'm not sure if I understand it well enough to say, but am very curious.