Making actor non reentrant

FWIW, I'm slowly myself arriving at a similar conclusion (albeit, in my case, I'm converging to the procedural style more).

I think that a big part of the problem is that there's not enough teaching on how to use actors "properly" (whether actors as implemented in Swift or actors as a broader concept); I observe that there's either the high-level documentation that "actors protect shared mutable state", or the low-level, but very abstract advice to think about transactionality, which comes up whenever people discover the reentrant behaviour.

But oftentimes people (myself included) just don't understand what to do with this, as Swift Concurrency is still so fresh that there simply aren't good examples of its usage in typical apps, and my suspicion that people just assume it's enough to change class into actor and keep the OOP implementation of the former class, where I would argue that actors should not be understood as objects, but rather data structures.

Indeed, when working with a typical out-in-the-wild struct (say Dictionary<Int, String>, or a wrapper around it for upholding some additional invariant, let's assume), we do not expect to be able to shove tons of functionality into it in the "classic" OOP manner and make it a service/provider/manager/factory; as it is understood that it's just a data container, and all the high-level business logic goes elsewhere.

I surmise that the same exact thinking should be applied to actors: they work much better as (possibly generic) data structure-y types that do not contain much business logic.

In this case, transactionality requirements are as self-evident as that of a dictionary inserting both the key and the value in one atomic operation, and the reason why this way of thinking is perhaps not as immediate is that actors being reference types sways people into incorrectly assuming that actors "are just thread-safe classes" too easily.

In any case, I've recently been experimenting with refactoring a project this way, and found much, much more success with actors while following this approach.

10 Likes