Type Methods

What type methods are

Methods are functions associated with an object type that return another object. They are either invoked on a particular object that conforms to the method's type, or (in the case of class methods) on no object. Methods can also take additional arguments.

The definitions of basic methods are part of the definition of a type.

The results of some methods can depend on context. For example, the result of an "age" method may vary depending on the time when the method is invoked.

No side effects defined

In the TOP system, all methods are call-by-value. There is no explicit notation of mutation. Therefore, you get information out of methods from the object a method returns. A method that completes successfully will always return a value.

The definitions of basic methods are part of the definition of a type.

How methods are defined

Method definitions contain the following information: Method descriptions contain the complete definition of a method. They can also contain the following additional information:

How methods are inherited

A type inherits the basic and derived methods of all its supertypes. Objects of type T, therefore, must have all the attributes specifically defined for type T, plus all of the attributes defined in T's supertypes, and so on. Class-based methods are not inherited.

A type does not have to redefine the method names that are defined in its supertypes. It may do so, however, if it wishes to define the method in a more constrained way, or describe the derivation differently. Constraints imposed by the supertype's definition must still be honored in the subtype's definition, if the method name is reused. In particular, this means that the return type may contravary, but the arguments must covary.

It is all right to have two supertypes that define two different methods under the same name. But any invocation of the method must specify not only the proper name, but the name of the type in which the desired method is defined. (The name of the type can be omitted if the method name alone is unambiguous.) The method name can also be unambiguously redefined in the subtype if the subtype's definition can conform to both supertype definitions. If no such definifion is possible, the method name may not be redefined.

How attributes differ from methods

See this list.

Basic, derived, and class-based methods

The essential methods of a type are known as "basic" methods. They have some degree of independence from each other. Basic methods are always called on a particular object.

In some cases, certain methods may be labeled as "derived". These are also called on a particular object, but the results of these methods are completely dependent on the basic attributes and methods. For instance, from the basic integer method "successor", "predecessor", and "is_zero", one can define derived methods for addition and subtraction. The semantics for derived methods should explain how to obtain the derived method from the basic methods. (Agents are free to implement the methods more efficiently if they wish, though. It would be extremely slow to implement addition by repeated succession, though it is possible to do so if required.)

Derived methods can be very useful, since many objects have simple definitions, but many commonly invoked operations that are based on the basic operations. Also, derived methods can be added to a type description without changing its basic definition.

(We don't handle class-based methods yet.)

spok@cs.cmu.edu (Last updated 29-Dec-94)