Re-use Variables

The re-use of code is unrelated to the derivation concepts of Timor. Instead Timor introduces a new concept called reuse variables.

The important difference between a re-use variable and a normal variable is that the compiler compares the definitions of its interface methods with those of the type being implemented. If some of these have matching signatures, it uses the methods of the reuse variable to implement them, unless the programmer has also declared the same method explicitly in the instance section. In the latter case the explicit method in effect overrides that provided by the re-use variable. If more than one reuse variable is declared in an implementation there are rules to resolve any clashes.

Interface methods of a re-use variable which do not match the type definition are ignored. However they can be invoked in an implementation by programmers.

Reuse variables are like normal variables in that they form part of the state of the implementation in which they are declared. They are recognisable because their declarations begin with a hat symbol (^). For example the following code fragment might appear in the state section implementation of a type DoubleEndedQueue:

^Queue myQueue = Queue::ListImpl();

Here the re-use variable myQueue is declared as a type variable and a list implementation constructor initialises it. In this case any implementation can be re-used, and the programmer has access to its interface methods.

A re-use  variable can also be declared as an implementation variable, e.g.

^Queue:: myQueue = ArrayImpl myQueueImpl = Queue::ArrayImpl(100);

in which case the rest of the implementation in which it is embedded can access its internal variables and methods.

In the case of a reusing a typeless implementation this is simply declared as

^::usefulCode

The type of a re-use variable does not necessarily have any formal relationship with the type being implemented, except that some (or all) of its interface methods may have the same signature.

In the case of derived types a re-use variable can imitate the standard OO techniques of delegation and normal subclassing. Since there is no relation between the type of the re-use variable and the type in which it is embedded, the former can be a subtype or a supertype of the latter or it may implement a formally unrelated type. For example given an implementation of a type Person this might be re-used to implement a type Student, as follows:

impl Student::Impl {
state:
^Person aPerson = Person::Impl();
...
instance:
/* the Student public methods added in the subtype
    are implemented here. The methods of Person can also be
    overridden */
}

Since any implementation in Timor can be a complete implementation of a type (without using re-use variables), an implementation of Student might be coded independently of an implementation of Person. In this case the re-use relationship can be reversed, e.g.

impl Person::Impl {
state:
^Student aStudent = Student::Impl();
}