[Next] [Previous] [Up] [Top] [Contents] [Index]

Appendix B: The Common Lisp Object System

Methods

CLOS is built on the idea that many functions operate in different ways when called on different classes of objects. Any function that operates in more than one way is called a generic function, a Lisp function whose behavior depends on the parameters supplied to it. Like any other Lisp function, a generic function can be passed as an argument and returned as a value, and it does all the other things a function does.

But while an ordinary function has a single body of code that is always executed, a generic function has a set of bodies of code, called methods. When a generic function is called on an instance, the function looks at the class or classes of the instance. In the simplest case, the generic function has a method for the instance's class. (For example, the generic function #'set-part-color has a method for table-dialog-item. Calling #'set-part-color on an instance of table-dialog-item calls that method. Calling #'set-part-color on an instance of radio-button-dialog-item calls a different method.)

In cases where the instance's class has a complex parentage, methods may be combined. What the function does--which bodies of code are called and how they are combined--is calculated from the class precedence list, the total ordering of the set of classes from which a class inherits. It also depends on the method combination type.

You can define a generic function by using the macro defgeneric. More often, however, you use defmethod, which defines a method on a generic function for a particular class. If the generic function does not already exist, defmethod creates one.

Defining a method and creating a generic function
Congruent lambda lists
Defining methods on instances
Creating and using accessor methods
Customizing initialization with initialize-instance
Creating subclasses and specializing their methods

Gettmg Started with MCL - 19 OCT 1996
[Next] [Previous] [Up] [Top] [Contents] [Index]

Generated with Harlequin WebMaker