April 2002
CLOS Overview
This document is to provide a short overview of important CLOS advantages. This list is short because most other OO systems have CLOS features.
Dynamic Multimethod Dispatch
Although most OO languages allow multimethods, very few have implemented dynamic dispatch on parameters. Multimethods are discussed in more detail here.
Constructor Advantages
CLOS has an efficient syntax for providing default field values (:initform) and for providing optional parameters for object constructors (:initarg).
(defclass person ()
((name :accessor person-name
:initform 'bill
:initarg :name)
(age :accessor person-age
:initform 10
:initarg :age)))
The :accessor is used as the name to access the field and can be set to any value. This is gives more flexibility, but in most cases in redundant: Note "person" is used three times, and both "name" and "age" twice for declaring the same object.
We could get rid of CLOS naming redundancy using macros and provide a more efficient class definition.
class person {
String name="bill" :init;
int age=10 :init;
)//person
Default values are provided, and the :init option declares that a constructor exists that can override the default value.
Java has a real problem with optional constructor arguments. C++ uses default arguments to avoid this problem.
class person{
String name
int age
public person(){
name="bill";
age=10l;
}//person
public person(String name){
this()
this.name=name;
}//person
public person(int age){
this()
this.age=age;
}//person
public person(String name, int age){
this.name=name;
this.age=age;
}//person
}//person
More Method Combinations
CLOS has an interesting ability to modify methods in an incremental way beyond OO function specialization. This definitely useful in a debugging context or an incremental development project.
The idea is to redefine the function by prepending or appending other methods. I use the nomenclature "f() g()" to mean "f is executed first, and then g". We use the following function as a basis for our examples:
(defmethod f () (print "f"))
:before same as f() = g() f()
(defmethod f :before () (print "g"))
:after same as f() = f() h()
(defmethod f :after () (print "h"))
:around same as f() = g() f() h()
(defmethod f :around () (print "g") (call-next-method) (print "h"))
Furthermore, there is no limit to the number of subsequent prefixes and suffixes you can add.
First draft April 2002