Method Command Example

Details

Action

Declares a
Method for an object type in the MathScript Object Hierarchy. Objects communicate with one another by sending messages. Each message is a request that the recipient of the message implement one of its methods. The methods themselves belong to the Type and not the actual Object. While this command declares the method name, the actual definition of the method is done first with a Program statement (see below).

Types of objects form a tree with each type subordinate to a number of parent types (or to none, if the type is a root). Object types have
instance variables of their own, and inherit the instance variables of their parents and other ancestors. Each type of object may access (get or set) these variables by sending itself or other types of objects messages that refer to built-in methods. For example, if an object has instance variable: color, then the methods putcolor and color are built-in. They respectively set the value of color, or get the value of color. If the type has an instance variable with the same name as a parent, the local variable overrides the parent variable.

All other Methods for taking actions must be declared with the Method Command, once those methods are created by writing a program. When an object receives a message to implement a method, it looks first to see if its type has the method to handle it. If not, it begins a breadth-first search among parent types until it finds a type with the appropriate method. Again, if type type has a method with the same name as a parent method, the local method overrides the parent method.

For example, the following declarations establish object types, and define methods for the types. Note that no object is actually created:

Type screen list(window, objs, selection, numobjs) list();

type geobj list(border, fill, width, center, selected, vertices, pname) list();

type poly list(order) list(geobj);

type circle list(order, radius) list(geobj);

Here, the programs dobuildpoly , dobuildobj, and docreateobject are are actually defined. The program defining the method should be created before the method is declared.

Notice that in the docreateobject program an object is actually created in the screen type's method.

program dobuildpoly (ex nam) {

let num be evaluate(text("numverts"));

let verts be getpoints(true,num);

send self putorder num;

let tv be translate(verts);

send self putvertices tv;

send self putborder bcolor;

send self putfill bcolor;

send self putwidth bwidth;

let cen be get(self,getcenter);

send self putcenter cen;

send self putpname nam;

send self draw false;

}

program dobuildobj (ex nam) {

<body>

}

program docreateobject (ex typ) {

let u be get(self,objs);

let n be get(self, numobjs);

let name be concat(typ,n);

object name typ list();

send self putobjs node(name, u);

send self putnumobjs n+1;

send name build name;

}

And here, the methods are declared . Note that Type Poly will use its own implementation of build (dobuildpoly) and that type Circle will simply inherit the generic implementation of build from the parent Type Geobj (dobuildobj).

method geobj build dobuildobj;

method poly build dobuildpoly;

method screen createobject docreateobject;

The Method command simply declared the name of the Method, and associated it with the name of a program. Different types may use the same name for their methods, but the programs themselves must be built first, and they must have distinct names, otherwise those names are arbitrary (We have affixed do to the method name in some cases for clarity only).