[Next] [Up] [Previous]
Next: Accessibility Up: Some CLOS terminology Previous: CLOS concepts

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 [tex2html_wrap6174] is a direct superclass of a class [tex2html_wrap6176] if [tex2html_wrap6178] explicitly designates [tex2html_wrap6180] as a superclass in its definition. In this case, [tex2html_wrap6182] is a direct subclass of [tex2html_wrap6184]. [tex2html_wrap6186] is a superclass of [tex2html_wrap6188] if there is a series of classes [tex2html_wrap6190] such that [tex2html_wrap6192] is a direct superclass of [tex2html_wrap6194] for [tex2html_wrap6196]. In this case, [tex2html_wrap6198] is a subclass of [tex2html_wrap6200].

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 [tex2html_wrap6202] is the union of the sets of names of slots defined by [tex2html_wrap6204] 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.

A brief online CLOS tutorial by Jeff Dalton, Email: J.Dalton@ed.ac.uk, is available in FTP://aiai.ed.ac.uk/lisp/random/clos-guide and from the Common Lisp repository as FTP://ftp.cs.cmu.edu/user/mkant/Lisp/doc/intro/clos-gd.txt.

[Next] [Up] [Previous]
Next: Accessibility Up: Some CLOS terminology Previous: CLOS concepts

TV Raman
Thu Mar 9 20:10:41 EST 1995