Bill Clementson's Blog

Bits and pieces (mostly Lisp-related) that I collect from the ether.

August 2005
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Jul  Sep

Long Form of define-method-combination in CLOS

Monday, August 1, 2005

Sonja Keene's book Object-Oriented Programming in Common Lisp: A Programmer's Guide to CLOS is generally regarded as the best introduction to CL's object system. One of CLOS's powerful features is that it makes it fairly simple to define new operator method cominations. In Keene's book, she describes both a short form and a long form for doing this. The short form she describes in detail; however, she really only mentions the long form:

"The long form of define-method-comination supports a rich and powerful syntax for defining a new framework. You can use it when none of the built-in method combination types (including standard) are appropriate, and the framework cannot be defined with the short form of define-method-comination. Because we believe that most applications will fit well with one of the built-in method combination types, we do not cover the syntax of the long form of define-method-comination in this book. See the CLOS specification for more information."
Although the short form is more suited for most requirements, the long form is more powerful in that arbitrary control structures can be implemented. However, it is difficult to find any good explanations or examples of long form usage. Section 28.2 in Steele's CLtL2 has some examples comparing the short and long forms, but good examples of long form usage seem to be hard to find. Alan Crowe has recognized this and attempted to rectify the situation by writing an introduction to CLOS using method combinations and macros as the underlying theme. He explains his rationale for writing the introduction on c.l.l.:
"There are two forms of define-method-combination, a short one and a long one. The relationship is a bit like the relationship between define-modify-macro and get-setf-expansion.

If I recall correctly Kleene explains the short form then makes a patronising comment about the long form being complicated and unnecessary, and then stops.

She might very well be right about the long form being complicated and unnecessary, I don't have the experience to judge. I'm happy for a book about CLOS to be opinionated and to get the benefit of the authors experience about which features are best tucked away in an appendix and forgotten about. However I expect a specialist book dedicated to CLOS to be complete.

I personally feel that studying the long form of define-method-combination was illuminating even if I never get aroung to using it. It foregrounds the question:
How do I exploit the fact that most of the code for my new method is already present in existing methods higher up the class hierarchy?
If I cannot reuse the code with call-next-method or with the built-in method combinations then I have a choice to make. Should I try to design a custom method combination or should I write my new method from scratch? Even if the built-in method combinations covers every combination worth using I nevertheless feel that my design benefits from asking a good question."
It would be neat to see some more examples of long form usage. Hopefully, Alan will expand on this tutorial at some stage in the future.

emacs Copyright © 2005 by Bill Clementson