In our new programming language, classes can define states. For example, a LightSwitch
is always either Off
or On
(never both):
class LightSwitch {
state On;
state Off;
}
Methods, including constructors, can change the state of this
with the ->
operator:
LightSwitch() { // constructor
->Off; // Transition to Off state.
}
Methods can specify what states the object must be in before they can be invoked and what states the object will be in after they exit by annotating the this
parameter. Constructors can specify what state they end in. However, states can only be specified on owned
references.
owned LightSwitch@Off() { // constructor always ends with the object in Off state
->Off;
}
void turnOn(owned LightSwitch@Off >> owned LightSwitch@On this) // turnOn() can only be called on objects that are in Off state.
{
->On;
}
void turnOff(owned LightSwitch@On >> owned LightSwitch@Off this)
{
->Off;
}
Each object can have one reference that statically specifies what state the object is in (since there can be only one owner of each object). For example, owned LightSwitch@On
is the type of a variable that refers to a switch that is in On
state.
void foo() {
LightSwitch s = new LightSwitch();
s.turnOn();
}
The compiler checks method invocations to make sure they are safe:
void foo() {
LightSwitch s = new LightSwitch();
s.turnOff(); // COMPILE ERROR: turnOff() requires that s is On, but here s is Off
}
As before, the programmer can use []
statements to check state information at compile time. For example, [s@Off];
will cause a compiler error if s
does not refer to an object in Off
state.
Unowned
. For example:
void foo(LightSwitch@Unowned s) {
s.turnOff(); // COMPILE ERROR: can't change state of s through an unowned reference
}
shared
. These references can be used to change the state of the referenced object, but invoking methods that can only be called in some states requires a runtime check. For example:
void test1(shared LightSwitch s) {
s.turnOn(); // COMPILE ERROR: turnOn requires that s be Off, but s is shared.
}
In the above situation, the programmer might need to check the state dynamically with if...is
.
is
void test2(shared LightSwitch s) {
if (s is On) { // runtime check to see whether the object referenced by s is in state On
s.turnOff(); // OK due to runtime check
}
}
Within the scope of the if...is
block, the compiler requires that if there s
, then the owner's state specification is never violated. If it is, then the program is terminated; it is up to the programmer to make sure the body of the if is
block does not change the state inappropriately.
When a shared
reference is needed, an owned
suffices as long as the reference is NOT to an asset. For example, an owned
reference can be passed as an argument to a method that expects a shared
reference to a non-resource object. However, the caller is left with a shared
reference.
When an unowned
reference is needed, any reference suffices, and the caller is left with their original kind of reference.