Actor inheritance
An actor can inherit another actor's attributes and methods. The child is also a subtype of its parent, so a child instance can be used wherever a parent instance is expected. A child instance has one actor identity and message queue. Both inherited and newly defined methods operate on its state.
Declaring a parent
Write the ancestry after the constructor parameters, separated by <.
For a concrete parent, supply its constructor arguments there:
actor CounterView:
add: action(int) -> int
def tick() -> int:
return add(1)
actor Counter(start: int) < CounterView:
var count = start
def add(amount: int) -> int:
count += amount
return count
pure def initial() -> int:
return start
actor ScaledCounter(start: int, scale: int) < Counter(start + 1):
def add(amount: int) -> int:
return Counter.add(amount * scale)
pure def requested() -> int:
return start
actor main(env: Env):
counter = ScaledCounter(10, 2)
view: Counter = counter
def __startup__():
assert counter.requested() == 10
assert view.initial() == 11
assert view.tick() == 13
env.exit(0)
Parentheses around the ancestry are optional:
actor ScaledCounter(start: int, scale: int) < (Counter(start + 1)):
has the same meaning.
Every actor ultimately inherits the built-in abstract actor Actor.
Omitting the ancestry, or writing < (), selects Actor as the base.
Writing < without a following base or () is a syntax error.
Actor inheritance is single inheritance. A regular class cannot be an actor's base. Although the ancestry syntax accepts a list of bases, multiple actor bases and direct protocol conformance are not supported.
Inherited members and overriding
Inside a child actor, inherited attributes and methods are available by their ordinary names, including private members. Public members remain available through an actor reference; inheritance does not expose private state to other actors.
Constructor parameters belong to the actor declaration that introduced
them. They remain available to that actor's methods, but do not enter a
child's scope. In the example, Counter.initial retains the parent's
start, whose value is 11, while ScaledCounter.requested uses the
child's start, whose value is 10. To expose a constructor argument to
children, bind it to an attribute in the parent.
Defining a method with an inherited name overrides it. Calls through an
actor reference and bare calls from inherited methods select the child's
override. Thus the tick() inherited from CounterView calls
ScaledCounter.add().
Overrides must satisfy the inherited method's type. Attribute overrides
must preserve their types and whether they are constants or mutable state.
To invoke an ancestor's implementation explicitly, qualify the method
with that actor's name, as in Counter.add(amount * scale). The current
actor is the implicit receiver; do not pass self. This is a local call
on the same actor. Such a reference is only valid inside a descendant
actor and must select a method with an implementation. It can also be
stored or passed as a callback.
Abstract actors
Omit the constructor parameter list to declare an abstract actor. In
particular, actor Example: is abstract, while actor Example(): is
concrete even though it takes no arguments.
An abstract actor can declare required attributes and method signatures,
and provide default method implementations. In the example, CounterView
requires the action add and provides the default method tick. Counter
implements add and inherits tick. A signature such as title: str would
instead require an attribute.
An abstract child may leave requirements unimplemented; a concrete child must implement every remaining requirement before it can be instantiated. Abstract actors themselves cannot be instantiated.
Abstract actors have no constructor arguments and cannot contain attribute
initializations. They can inherit other abstract actors, but cannot
inherit concrete actors. Use a bare base name such as CounterView, rather
than CounterView(), when inheriting an abstract actor.
Type parameters
Actors can have type parameters. A child supplies
the parent's type arguments before its constructor arguments. For example,
a parent declared as actor GenericParent[T](x: T): can be selected with
< GenericParent[int](item) in the child's declaration.
The same type-argument syntax applies to abstract bases, without a constructor call. Protocol bounds on type parameters work as they do for classes: the child's declared bounds must justify the parent's bounds.
Initialization and startup
Constructing a child allocates one actor. The parent constructor arguments
are evaluated, the parent is initialized, and then the child's declarative
bindings are evaluated. This repeats through the ancestry, so inherited
attributes are initialized before the child's bindings. The usual
initialization-safety checks still apply to self and callbacks referring
to the actor under construction.
After successful construction, the runtime automatically schedules one
__startup__() message for the new actor. Startup is an ordinary inherited
and overridable method, with signature action() -> None. If a child
inherits its parent's implementation, that implementation runs. If the
child overrides it, only the override is scheduled; call
Parent.__startup__() inside the override when the parent's startup work
is also needed. The built-in Actor supplies a no-op default.
Startup runs asynchronously, outside the construction transaction. Do not call it explicitly in the declarative body, and do not assume it has finished when the constructor returns.