initargs
for now.
186
Adding an :after
method to INITIALIZE-INSTANCE
is the Common Lisp analog to defining a constructor in Java or C++ or an __init__
method in Python.
187
One mistake you might make until you get used to using auxiliary methods is to define a method on INITIALIZE-INSTANCE
but without the :after
qualifier. If you do that, you'll get a new primary method that shadows the default one. You can remove the unwanted primary method using the functions REMOVE-METHOD
and FIND- METHOD
. Certain development environments may provide a graphical user interface to do the same thing.
(remove-method #'initialize-instance
(find-method #'initialize-instance () (list (find-class 'bank-account))))
188
Of course, providing an accessor function doesn't really limit anything since other code can still use SLOT-VALUE
to get at slots directly. Common Lisp doesn't provide strict encapsulation of slots the way some languages such as C++ and Java do; however, if the author of a class provides accessor functions and you ignore them, using SLOT-VALUE
instead, you had better know what you're doing. It's also possible to use the package system, which I'll discuss in Chapter 21, to make it even more obvious that certain slots aren't to be accessed directly, by not exporting the names of the slots.
189
One consequence of defining a SETF
function—say, (setf foo)
—is that if you also define the corresponding accessor function, foo
in this case, you can use all the modify macros built upon SETF
, such as INCF
, DECF
, PUSH
, and POP
, on the new kind of place.
190
The 'variable' names provided by WITH-SLOTS
and WITH-ACCESSORS
aren't true variables; they're implemented using a special kind of macro, called a WITH- SLOTS
and WITH-ACCESSORS
, but you can also use them for your own purposes. I'll discuss them in a bit more detail in Chapter 20.
191
The Meta Object Protocol (MOP), which isn't part of the language standard but is supported by most Common Lisp implementations, provides a function, class-prototype
, that returns an instance of a class that can be used to access class slots. If you're using an implementation that supports the MOP and happen to be translating some code from another language that makes heavy use of static or class fields, this may give you a way to ease the translation. But it's not all that idiomatic.
192
In other words, Common Lisp doesn't suffer from the
193
Of course, most folks realize it's not worth getting that worked up over FORMAT
's control strings is character based, while the extended LOOP
macro can be understood only in terms of the grammar of the LOOP
keywords. That one of the common knocks on both FORMAT
and LOOP
is that they 'aren't Lispy enough' is evidence that Lispers really do like the s-expression syntax.
194
Readers interested in the pretty printer may want to read the paper 'XP: A Common Lisp Pretty Printing System' by Richard Waters. It's a description of the pretty printer that was eventually incorporated into Common Lisp. You can download it from ftp://publications.ai.mit.edu/ai-publications/pdf/AIM- 1102a.pdf
.