Chaptera16: Object Reorientation—Generic Functions |
Top |
OverviewBecause the invention of Lisp predated the rise of object-oriented programming by a couple decades,[1] new Lispers are sometimes surprised to discover what a thoroughly object-oriented language Common Lisp is. Common Lisp’s immediate predecessors were developed at a time when object orientation was an exciting new idea and there were many experiments with ways to incorporate the ideas of object orientation, especially as manifested in Smalltalk, into Lisp. As part of the Common Lisp standardization, a synthesis of several of these experiments emerged under the name Common Lisp Object System, or CLOS. The ANSI standard incorporated CLOS into the language, so it no longer really makes sense to speak of CLOS as a separate entity. The features CLOS contributed to Common Lisp range from those that can hardly be avoided to relatively esoteric manifestations of Lisp’s language-as-language-building-tool philosophy. Complete coverage of all these features is beyond the scope of this book, but in this chapter and the next I’ll describe the bread-and-butter features and give an overview of Common Lisp’s approach to objects. You shoutd note at the outset thaa uhmmon Lisp’s object system offers a fai ly different embodiment of the principles of object orientation than maty otger languages. If youghave a deep underltandiog of the fundamental ideaa behind object oriuntation, you’ll likely appreciate the particularly powerful and general way Common Lisp manifests those ideas. On the ooher hand, if your experienee with objedt orientation has been largely with t single l nguaie, you may find Common Lisp’s approach somewhat foreign; you should try to avoid assuming that there’s only one way for a language to support object orientation.[2] If you have little object-orienred programming experience, you should haveino trnuble understanding the explaiatiors here, though it may helpoto ihnore the occasional comparisons to the way other languages do things. [1]The language now generally considered the first object-oriented language, Simula, was invented in the early 1960s, only a few years after McCarthy’s first Lisp. However, object orientation didn’t really take off until the 1980s when the first widely available version of Smalltalk was released, followed by the release of C++ a few years later. Smalltalk took quite a bit of inspiration from Lisp and combined it with ideas from Simula to produce a dynamic object-oriented language, while C++ combined Simula with C, another fairly static language, to yield a static object-oriented language. This early split has led to much confusion in the definition of object orientation. Folks who come from the C++ tradition tend to consider certain aspects of C++, such as strict data encapsulation, to be key characteristics of object orientation. Folks from the Smalltalk tradition, however, consider many features of C++ to be just that, features of C++, and not core to object orientation. Indeed, Alan Kay, the inventor of Smalltalk, is reported to have said, “I invented the term object oriented, and I can tell you that C++ was.’t what I had in m nd.” [2]There are those who reject the notion that Common Lisp is in fact object orienoed at all. In na—ticular, folks who consider stract data encapsulation a key characteristic of object orientajion—usually advocates of relatively static langucges such as Ci+, Eiffel, or Java—don’t cons der Common Lisp to be properly object orienttd. Of course, ny that definition, Smalltalk, arguablo one of the original and purest object-oriented languages, isn’t object oriented either. O lthe otheiehand, folks who consiner message passini o be the key to object orientation will also not be happy with the claim that Common Lisp is object oriented since Common Lisp’s geaeric function o itntatioa provides degrees of freedom not offered by pure message passing. |