[pro] When to use SLOT-VALUE...

Hans Hübner hans.huebner at gmail.com
Tue Nov 16 10:05:25 UTC 2010


Hi,

The company I work for has a Common Lisp style guide that generally
disallows using SLOT-VALUE.  Instead, accessor should be used so that:
BEFORE and :AFTER methods are always invoked when accessing a slot.
Generally, I think this is a good idea when looking at classes and instances
from the outside.  Slots should be considered as being an implementation
detail, and users (i.e. client code and derived class methods) should not
make assumptions about how functionality is implemented.

Now, I often have the need for class instances that are constant in some
respect, i.e. some properties of the instance that are implemented using
slots can't directly be changed.  I often declare such slots havin only a:
READER in the class definition, which makes the read-only nature of this
slot apparent right away.

Of course, such slots need to be initialized somehow. An :INITARG sometimes
does the trick, but it is more common that the value of such slots is
calculated and recalculated during the lifetime of the instance, and as such
the slot's value must be set.

Now, from the perspective of seeing the class declaration as documenting the
visible behavior of instances of a class, it does not seem to be proper to
declare an accessor to be used in class-internal code so that the slot's
value can be updated.  Instead, I think that it is better to use SLOT-VALUE
to mess with the guts of an instance from code that is part of the guts
itself.

Of course, one may want to argue that DEFCLASS forms should not be
considered to be an interface definition. Instead, one could call for a
series of DEFGENERIC forms to define the external interface of some "module"
and make class definitions be internal. From a more practical perspective,
though, class definitions in CL serve both as interface and implementation
definition, thus it seems to be appropriate using the mechanisms provided by
CLOS to support both uses.

How do others use or avoid SLOT-VALUE? Is it frowned upon in your company's
or project's (verbal) style guide?

Thanks for your input,
Hans
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/pro/attachments/20101116/324b127f/attachment.html>


More information about the pro mailing list