4.2.7. Changing the Search Order for Methods
You can change the usual search order for methods by:
Ensuring that the receiver object is the sender object. (You usually do this by specifying the special variable SELF, see
SELF
).
Specifying a colon and a class symbol after the message name. The class symbol can be a variable name or an environment symbol. It identifies the class object to be used as the starting point for the method search.
The class object must be a superclass of the class defining the active method, or, if you used SETMETHOD to define the active method, the object's own class. The class symbol is usually the special variable SUPER (see
SUPER
) but it can be any environment symbol or variable name whose value is a valid class.
Suppose you create an Account class that is a subclass of the Object class, define a TYPE method for the Account class, and create the Savings class that is a subclass of Account. You could define a TYPE method for the Savings class as follows:
savings~define("TYPE", 'return "a savings account"')
You could change the search order by using the following line:
savings~define("TYPE", 'return self~type:super "(savings)"')
This changes the search order so that the language processor searches for the TYPE method first in the Account superclass (rather than in the Savings subclass). When you create an instance of the Savings class (asav
) and send a TYPE message to asav
:
say asav~type
an account (savings)
is displayed. The TYPE method of the Savings class calls the TYPE method of the Account class, and adds the string (savings)
to the results.