Classes and instances must be able to hold state. TOM provides a few kinds of state.
implementation class StatefulCounter { int starting_value; } end; |
As with the Counter instance defined previously, we've defined a StatefulCounter, which possesses state. In this case though, we've created a class variable, which maintains a single value, accessible by all instances of that class. However, each sub-class of StatefulCounter would receive its own copy of starting_value that would be shared by its instances.
Various qualifiers may be applied to the declaration of an object variable. These qualifiers alter the behavior and scope of that variable.
mutable int starting_value; |
A mutable variable automatically defines a setter method of the form:
void set_starting_value int _new_value { starting_value = _new_value; } |
The compiler will generate the method using the correct type and variable names. This may be used in conjunction with the public qualifier.
obsolete int starting_value; |
An obsolete variable has been marked to warn programmers that it will be going away in the future. References to this variable will generate warnings. Additionally, if it is public or mutable, the methods generated will also be flagged as obsolete and use of those methods will generate warnings.
This does not completely work yet. Variable references do not yet generate warnings.
private int starting_value; |
A private variable is only accessible to methods on the class which defined it.
protected int starting_value; |
A protected variable is only accessible to methods on the class which defined it, as well as any sub-classes.
public int starting_value; |
The compiler automatically creates an accessor method for a public variable. The accessor is of this form:
int starting_value { = starting_value; } |
The compiler will generate the method using the correct type and variable names. This may be used in conjunction with the public qualifier.
static int starting_value; |
A static variable only exists upon the class which defined it. Sub-classes do not receive their own copy of the variable. The static qualifier is only valid on class variables.
static local int starting_value; |
A local variable's value is stored within thread-local storage. The local qualifier is only valid on static class variables.
redeclare long starting_value; |
Redeclaring a variable allows the programmer to change the type. Redeclaring a variable that was not previously declared is not allowed.