Re: CLOS class in different packages
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Tue, 05 Dec 2006 09:29:52 +0100
dneu@xxxxxxx wrote:
Hi,
I have a parent CLOS class and related generics defined in one
package, and have classes derived from the parent defined in other
packages. In order to access slots defined in the parent, from the
derived class, I had to either:
1. export them from the parent class, or
2. qualify them using the package name, e.g. parent::last-name
Option 1 seems to sort of "disconnect" the slot from it's class, and
option 2 seems somewhat clumsy.
Is there another option?
Yes: In general, it is better not to access slots directly via slot-value and the likes, but to rather go through accessor functions, which you can declare with :reader, :writer and :accessor options in the slot definitions. Just export the names of those accessor functions. This has the advantage that you can better control whether client code is allowed to only read or additionally write slots as well. It also has the advantage that you can define :before/:after/:around methods on such accessors that do additional things whenever such slots are accessed, and be sure that they are actually triggered. Furthermore, it gives you the option to later change your mind and, for example, instead of representing an attribute of a class as a slot turn it into a computed attribute. The functional interface that readers and writers provide don't need to be changed in that case, whereas access via slot-value becomes invalid then.
Finally, in most CLOS implementations, accessing slots via accessor functions is typically actually faster than with slot-value etc. (but at least not slower).
You should really leave access via slot-value etc. to low-level functionality that is close to the original class definition when you, for example, want to circumvent :before/:after/:around methods on accessors.
For that matter, it is probably a good idea that accessor functions have different names than the slots themselves.
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
.
- References:
- CLOS class in different packages
- From: dneu
- CLOS class in different packages
- Prev by Date: Re: Common Lisp from a Unix perspective - barriers to using CL
- Next by Date: UnSuggester haha.
- Previous by thread: Re: CLOS class in different packages
- Next by thread: Re: CLOS class in different packages
- Index(es):
Relevant Pages
|