| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (9 out of 12)

Invoking a Method

Method invocation is syntactically similar to procedure invocation.

To invoke a method:

For information about the call statement, see call. For information about the start action, see start. The differences between call and start are the same for methods and procedures.

Invoking a Method Generically

When you invoke a method generically, G2 selects the particular method to invoke.

To invoke a method generically:

To execute such an invocation, G2 does the following:

  1. Notes the class of the first argument in the call statement.

  2. Obtains the class inheritance path of that class.

  3. Scans the classes on the path looking for one that has a method that:

If G2 finds a class with such a method defined, G2 invokes the method, sending it the argument(s) specified in the invocation. If G2 reaches the end of the inheritance path without finding such a class, it signals an error.

For example, suppose that:

You could then invoke fill on a flask by executing:

The class flask and its parent vessel each has a fill method. Since every class appears first in its own inheritance path, G2 invokes the method for flask, sending it the arguments flask-1.

Matching Types in Generic Method Invocations

When you invoke a method generically, the class of the first argument and the number of arguments are significant, because they specify the class whose inheritance path G2 scans to search for a matching method, and the number of arguments that a matching method must have.

The types of any additional arguments, and the number and type(s) of any return values, are not significant for selecting which method to invoke. However, they must match whichever method G2 actually invokes, or G2 signals an error, as with a similar mismatch in an ordinary procedure invocation.

Invoking a Method Directly

In most cases, you invoke a method by specifying an operation, leaving G2 to select the correct method as described under Invoking a Method Generically.

Some situations require invoking a specific method and no other, bypassing G2's selection process. To allow such invocation, G2 provides a qualified name for every method. This name has the syntax:

For example, the qualified name of the fill method for flask is:

To invoke a method directly, you give its qualified name in a call statement or start action. G2 then invokes exactly the designated method. If the method does not exist, G2 signals an error: it does not search the inheritance path of the class specified in the generic name.

For example, suppose that:

You could obtain the described effect by executing:

To execute this statement, G2 acts just as it would for an ordinary procedure call: it calls the fill method for vessel on the object flask-1.

The call next method statement has the same effect whether the method that contains it was invoked generically or directly, as described under Invoking a Superior Method (call next method).

Optional Direct Invocation

You can use direct invocation even where generic invocation would have the same effect. Such invocation, though initially unnecessary, ensures that your code will always call the particular method despite subsequent changes to the class hierarchy. Using direct invocation does not protect against changes to the effect of executing call next method.

For example, so long as the class hierarchy described for the vessel example remains unchanged:

has the same effect as

but the former will always invoke flask::fill no matter how the class hierarchy changes, while the latter might cease to do so. However, such a change in the hierarchy might in either case change the effect of call next method.

Matching Types in Direct Method Invocations

When you invoke a method directly, all arguments and any returned values must match the invocation, or G2 signals an error, as with any procedure call.

Direct invocation allows you to invoke a method on an item whose class differs from that of the first argument defined by the method. Such an invocation is correct if the item belongs to a subclass of the argument class, but not if it belongs to a superior class.

That is, you cannot directly invoke a method on an item whose class is superior to the class for which the method is defined. If you attempt to violate this restriction, G2 signals an error.

This restriction exists because an instance of a superior class may not have all of the attributes needed by a method or procedure designed for use with an inferior class: additional attributes may be added lower in the hierarchy.

Invoking a Superior Method (call next method)

You can use the call next method statement to cause one method to call another that is defined for a superior class. The statement allows you to specify behavior hierarchically, as described under Designing a Class Hierarchy.

The syntax of the call next method statement is:

Note that this syntax is the same as that of an ordinary call statement, except that no arguments appear. For example, the flask::fill method shown earlier included:

G2 executes a call next method statement as follows:

If G2 finds such a method, G2 calls it, passing it the same arguments that the calling method received. If G2 does not find such a method, G2 does not signal an error; it proceeds to the next statement in the calling method.

A call next method statement is similar to an ordinary call statement. Specifically:

The call next method statement applies only to methods: it cannot appear in a procedure.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (9 out of 12)

Copyright © 1997 Gensym Corporation, Inc.