OK, so I want to preface this by saying that I really have no idea what I'm doing here. This idea has been bouncing around in my head for a while, and I think it could be useful. But I do not understand enough about the compiler, concurrency, or even the evolution process to do a good job here. Think of this like a pitch pitch!
I am willing to put in the work here. But, I really wanted to do a first-pass first to see if this is a) possible and b) a good idea.
So here's the problem, which I actually originally posted as a question:
class NonSendableType {
private var internalState = 0
func doSomeStuff() {
// must be on some actor here right???
Task {
let value = await otherType.getAValue()
// aren't I back on the actor that called me???
self.internalState += value // non-Sendable self captured here
}
}
}
The important bits: this type is not Sendable and has internal mutable state that must be protected. However, it also wants to do some async stuff. And it absolutely feels like this should be possible. I have ran into a number of situations where it would be really useful to have this kind of isolation guarantee.
A solution which I regularly use is to slap on a global actor annotation to this type. This works, but its very limiting. This type doesn't need to be statically tied to a global actor. It just needs to be used in some isolated context - the one that created it.
I would like to express this to the compiler!
Imaginary solution 1:
isolated class NonSendableType {
}
This would tell the compiler to disallow whatever situations prevent it from making the assumption that there may not be an isolation context. I'm just going to assume such a thing is possible. And that leads me to an even more wild idea:
I kinda think this should be the default behavior for all non-Sendable types!
My intuition says that non-Sendable types that are also ok to use without an isolation context of any kind are rare. And because such behavior would really help both flexibility and just general intuition how about requiring explicit opt-in:
Imaginary solution 2:
nonisolated class NonSendableType {
}
This would tell the compiler that it cannot assume there is isolation. This is how (I think) things work today.
I fear that this may actually be impossible, a bad idea, or both. But I thought it was at least worth a pitch pitch.