XLISP: An Object-oriented Lisp

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# XLISP Object System

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

2. The 'send' Function
3. The 'self' Symbol
4. The 'send-super' Function
5. The 'object' Class

- object - the top of the class hierarchy

- :show - show an object's instance variables
- :class - return the class of an object
- :isnew - the default object initialization routine
- send-super - send superclass a message

6. The 'class' Class

- class - class of all object classes

- :new - create a new instance of a class
- :isnew - initialize a new class
- :answer - add a message to a class

## 1 Definitions

selector | — | a symbol used to select an appropriate method, usually a keyword
message | — | a selector symbol and a list of actual arguments
method | — | the Lisp 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

## 2 The 'send' Function

You can send a message to an object using the send function:

(send object selector [args])
  object - an object
  selector - message selector for object
  arg - parameter sent to object method
  returns - the object

The send 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. It 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

## 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

## 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:

(send-super selector [args])
  selector - the message selector
  args - the optional message arguments
  returns - the result of sending the message

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

## 5 The 'object' Class

object - the top of the class hierarchy.

Messages:

(send object :show) - show an object's instance variables.
  returns - the object
(send object :class) - return the class of an object
  returns - the class of the object
(send object :isnew args) - run the default object initialization routine
  returns - the object
(send object :isa class) - test if object inherits from class
  returns - T if object is an instance of class or a subclass of class, otherwise NIL

Back to Top

## 6 The 'class' Class

class - class of all object classes (including itself)

Messages:

(send class :new ivars [cvars [super]]) - create a new instance of a class
  returns - the new class object
(send class :isnew ivars [cvars [super]]) - initialize a new class
  ivars - list of instance variable symbols
  cvars - list of class variable symbols
  super - the superclass, default is object
  returns - the new class object
(send class :answer selector fargs body) - add a message to a class
  selector - a message selector symbol
  fargs - the formal argument list, a lambda list
  body - 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

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference