Chapter 3 The Application Description Language

3.21 Method Definition

Method definitions are contained in blocks introduced by the keyword on followed by the message prototype. The prototype must begin with an identifier called the selector that is unique within the method's class. The selector appears as an unquoted character string as the first component of the message list (see Section 3.13, "Messages" page 30). The selector can be the same as the selector of a method in the parent class or one of its ancestors. In this case, the new method overrides the ancestor's method.

If the method receives arguments, you must declare these arguments in the message prototype after the selector and a colon separator. The arguments must be of base or compound type. Neither arrays nor objects can be message arguments, but handles to them are allowed. The type of the arguments, if they are present, are checked each time a method is invoked. Message arguments may be unset.

The ADL does not allow the overloading of selectors. That is, a method is always called with the same type of arguments in the same order. The same method selector cannot be specified for two separate methods that are distinguished by their argument lists, as in C++. The method can return a value, once again of base or compound type. If it does so, you must declare the type of the return value as the last part of the prototype following all the argument declarations. This declaration takes the form of the keyword return followed by the type of the return value.

The block that follows the message prototype contains the executable ADL code that defines the method. If a method declares a return value, this code must indicate the return value using the keyword return followed by an expression that evaluates to the return value. The returned value may be unset. The method terminates upon execution of a return statement, or if the method does not return a value it terminates after execution of the last statement of the block. In methods with no return value, do not follow a return statement indicating premature termination by an expression.

In an object method, the keyword self is always a handle referring to the object and the keyword sender is always a handle to the object that sent the message currently being handled. If the message was sent by the system, e.g., a default or special constructor or an Init message, then sender is NULL. An object can directly set the value of an object member without using a Set_ message to self.

Methods, like object members, can be common, that is, they can apply to the class as a whole rather than a particular instance of the class (unimplemented). You can declare a common method by inserting the keyword common between on and the selector in the method definition. Common methods have access only to the common members of a class, not to the regular members, because you cannot refer to a regular member without referring to an instance.

You may declare a method to be local if you do not wish the method to be inherited by a subclass. That is, a local method may not be invoked by a message directed to an instance of a derived class.

See Figure 3.40 for the general form of a method definition. The colon is required if either an argument list or return type is present.
Method Definitions
on [common] [local] selector [:[type1 arg1, ... , typen argn] [return rettype]]
{
Method Declarations
Method Statements
}

The forward statement (unimplemented) indicates that the current message is to be forwarded to another object. The statement has two forms illustrated in Figure 3.41.
Two Forms of the Forward Statement
forward => object/handle;
forward stringSelector => object/handle;

In both cases, execution transfers to a method in the new target specified by the object or handle appearing after the (=>) operator. The first case of this example issues an identical message to the new target. The second case uses the same arguments, but specifies a new selector by a string. The new target appears to return directly to the message sender in the case of a synchronous two-way message, not to the forwarding object. An object can forward a message to a different method within itself by using the construction

forward 'newSelector => self;

The forward statement is largely syntactic sugar except that it guarantees that the message is forwarded synchronously, even if the original message is asynchronous. That is, once a method has started to handle a message, the forward statement is seen as an extension of the original handler, and not the transmission of a new message.


AM2 Documentation - 19 NOV 1996

Generated with Harlequin WebMaker