IMHO add adding such API on "all" actors is going to encourage bad patterns, and that's why I had argued against it way back then (it was suggested as .run back then).
We such API (MainActor.run{}) it specifically on MainActor because yeah it's an actor that you do often just need for "ok, definitely run on this one" and only for reasons of threading. It also is a global actor that tends to sprawl all over an application so it made sense to ease this specific pattern with it.
The problems with offering such API are numerous, primarily:
-
It makes it harder for people to switch their thinking about actors from "oh it's a queue" and just slap everything into an
await actor.run { a in lots of logic }rather than expressing the domain's actual actions the actor is taking as explicit methods "doManyThings()" vs "ok whatever I'll justawait actor.run { a.one(); await a.two() }1) -
It makes reentrancy hard to spot and control again -- presumably the "run" would be taking an async closure; so you think you got exclusive access "during the run { ... }" but you don't. If you write your own methods you can make them not async and therefore help yourself to not accidentally cause reentrancy issues (
func doManyThings() /* on purpose not async */) -
It also doesn't help the debugging crashes story:
"oh, we crashed in run, uhm..." versus crashing in exact named methods -- now we got better backtraces but still in release builds you don't always get very precise information i guess still... (and we don't do frame pointers by default on linux I believe so the problem is still a thing).
Still, having exact method names helps with e.g. profiling your system and you spot "yes, the getThings method is taking a lot of time" vs. "the run() is always taking a lot of time" because too much code was put into the "too easy to use" run {} pattern.
Overall, yes it is "easy" but it is not the best way to use actors and while everyone CAN write this function (call it run, or "with isolated") using a few simple lines of code -- we should not encourage this style as a whole. That's why IMHO it is not a good addition to the standard Actor type.