Thursday, March 18, 2021

Swift Proposal: Actors

SE-0306 (forum, Hacker News):

Actors allow you as a programmer to declare that a bag of state is held within a concurrency domain and then define multiple operations that act upon it. Each actor protects its own data through data isolation, ensuring that only a single thread will access that data at a given time, even when many clients are concurrently making requests of the actor. As part of the Swift Concurrency Model, actors provide the same race and memory safety properties as structured concurrency, but provide the familiar abstraction and reuse features that other explicitly declared types in Swift enjoy.

[…]

As a special exception to the rule that an actor can only inherit from another actor, an actor can inherit from NSObject. This allows actors to themselves be declared @objc, and implicitly provides conformance to NSObjectProtocol[…]

[…]

Like classes, actors as proposed allow inheritance. However, actors and classes cannot be co-mingled in an inheritance hierarchy, so there are essentially two different kinds of type hierarchies. It has been proposed that actors should not permit inheritance at all, because doing so would simplify actors: features such as method overriding, initializer inheritance, required and convenience initializers, and inheritance of protocol conformances would not need to be specified, and users would not need to consider them. The discussion thread on the proposal to eliminate inheritance provides several reasons to keep actor inheritance[…]

Previously:

3 Comments RSS · Twitter

So, these are classes that enforce immutability, right?

Sören: not at all. They are classes that enforce safe access to properties and safe execution of methods. Basically by making them all atomic and/or enforcing async calls (not clear on that last one). But they are definitely mutable. Also an actor is a reference type, unlike struct and enum, which are value types and for which an equivalent setup would not make sense.

The ghost of Carl Hewitt walks, even if he's not dead yet. I haven't heard about actors since the late 1970s. It's amazing how many great ideas from the 1960s and 1970s are showing up again, and many of them are still great ideas.

Leave a Comment