XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next

10  Objects


  1. Definitions
  2. The 'send' Function
  3. The 'self' Symbol
  4. The 'send-super' Function
  5. The 'Object' Class
  6. The 'Class' Class

10.1  Definitions


selector   -   a symbol used to select an appropriate method
message   -   a selector and a list of actual arguments
method   -   the code that implements a message
object   -   the top of the class hierarchy
class   -   the class of all object classes (including itself)

Since XLISP was created to provide a simple basis for experimenting with object-oriented programming, one of the primitive data types included is object. In XLISP, an object consists of a data structure containing a pointer to the object's class as well as an array containing the values of the object's instance variables.

Officially, there is no way to see inside an object [look at the values of its instance variables]. The only way to communicate with an object is by sending it a message.

  Back to Top


10.2  The 'send' Function


You can send a message to an object using the send function. This function takes the object as its first argument, the message selector as its second argument [which must be a symbol] and the message arguments as its remaining arguments.

The send function determines the class of the receiving object and attempts to find a method corresponding to the message selector in the set of messages defined for that class. If the message is not found in the object's class and the class has a super-class, the search continues by looking at the messages defined for the super-class. This process continues from one super-class to the next until a method for the message is found. If no method is found, an error occurs.

  Back to Top


10.3  The 'self' Symbol


When a method is found, the evaluator binds the receiving object to the symbol self and evaluates the method using the remaining elements of the original list as arguments to the method. These arguments are always evaluated prior to being bound to their corresponding formal arguments. The result of evaluating the method becomes the result of the expression.

Within the body of a method, a message can be sent to the current object by calling:

(send self ... )

The method lookup starts with the object's class regardless of the class containing the current method.

  Back to Top


10.4  The 'send-super' Function


Sometimes it is desirable to invoke a general method in a superclass even when it is overridden by a more specific method in a subclass. This can be accomplished by calling send-super, which begins the method lookup in the superclass of the class defining the current method rather than in the class of the current object.

The send-super function takes a selector as its first argument [which must be a symbol] and the message arguments as its remaining arguments. Notice that send-super can only be sent from within a method, and the target of the message is always the current object self.

(send-super ... )

is similar to:

(send self ... )

except that method lookup begins in the superclass of the class containing the current method rather than the class of the current object.

  Back to Top


10.5  The 'Object' Class


object - the top of the class hierarchy.

Messages:

:show - show an object's instance variables.
returns - the object

:class - return the class of an object
returns - the class of the object

:isnew - the default object initialization routine
returns - the object

send-super sel args... - send superclass a message
sel - the message selector
args - the message arguments
returns - the result of sending the message

  Back to Top


10.6  The 'Class' Class


class - class of all object classes (including itself)

Messages:

:new - create a new instance of a class
returns - the new class object

:isnew ivars [cvars [super]] - initialize a new class
ivars - the list of instance variable symbols
cvars - the list of class variable symbols
super - the superclass (default is object)
returns - the new class object

:answer msg fargs code - add a message to a class
msg - the message symbol
fargs - the formal argument list (lambda list)
code - a list of executable expressions
returns - the object

When a new instance of a class is created by sending the message :new to an existing class, the message :isnew followed by whatever parameters were passed to the :new message is sent to the newly created object.

When a new class is created by sending the :new message to the object class, an optional parameter may be specified indicating the superclass of the new class. If this parameter is omitted, the new class will be a subclass of object. A class inherits all instance variables, class variables, and methods from its superclass.

  Back to Top


XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next