Using CLOS

A class can inherit structure and behavior from other classes. A class whose definition refers to other classes for the purpose of inheriting from them is said to be a subclass of each of those classes. The classes that are designated for purposes of inheritance are said to be superclasses of the inheriting class.

A class C1 is a direct superclass of a class C2 if C2 explicitly designates C1 as a superclass in its definition. In this case, C2 is a direct subclass of C1. Cn is a superclass of C1 if there is a series of classes C2,,Cn1 such that Ci+1 is a direct superclass of Ci for 1 i < n. In this case, C1 is a subclass of Cn.

A class can inherit slots (named fields) and methods from its superclasses. A subclass inherits methods in the sense that any method applicable to all instances of a class is also applicable to all instances of any subclass of that class. The set of names of all slots accessible in an instance of a class C is the union of the sets of names of slots defined by C and its superclasses.

Like an ordinary Lisp function, a generic function takes arguments, performs a series of operations, and perhaps returns useful values. An ordinary function has a single body of code that is always executed when the function is called. A generic function has a set of bodies of code, of which a subset is selected for execution. The selected bodies of code, called the effective method, and the manner of their combination are determined by the classes or identities of one or more of the arguments to the generic function and by its method combination type.

In standard method combination, primary methods are unqualified methods and auxiliary methods are methods with a single qualifier that is one of :around, :before, or :after. Primary methods define the main action of the effective method, while auxiliary methods modify that action in one of three ways.

  1. :before Method specifies code to be executed before applicable primary methods. Applicable before methods are executed in most-specific-first order.
  2. :after Method specifies code to be executed after all applicable primary methods. After methods are executed in most-specific-last order.
  3. :around Method specifies code to be run instead of applicable primary methods. The most specific around method is called first, and it can pass control to other around methods and, when no other around method is applicable, to the most-specific primary method.

The method combination facility controls the selection of methods, the order in which they are run, and the values that are returned by the generic function. CLOS offers a default method combination type, called standard method combination, and provides a facility for declaring new types of method combination.

An online CLOS tutorial by Jeff Dalton, E-mail: J.Dalton@ed.ac.uk, is available at URL FTP://aiai.ed.ac.uk/lisp/random/.