Re: CLOS class in different packages



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/
.



Relevant Pages

  • Re: Pass variables from parent DTS to child DTS package...
    ... Pass variables from parent DTS to child DTS package... ... I have a parent DTS that calls a child DTS through ActiveX scripts. ...
    (microsoft.public.sqlserver.dts)
  • RE: Vista update problem
    ... In my case it was the anti virus client causing KB941649 not to install ... Appl: detect Parent, Package: ... Appl: detectParent: package: ...
    (microsoft.public.windowsupdate)
  • Sequence of SSSI Package Configuration.
    ... TYPE: from parent package, ... I need to load 1st the connectionString to configuration DB then connect to ... Information: 0x40016040 at DumpAccountingToFile: The package is attempting ...
    (microsoft.public.sqlserver.dts)
  • RE: Vista update problem
    ... Appl: detect Parent, Package: ... Appl: detectParent: package: ... updates to install them manually ...
    (microsoft.public.windowsupdate)
  • Re: CLOS class in different packages
    ... and have classes derived from the parent defined in other ... qualify them using the package name, ... `(let,(loop for slot-var in slot-vars ... and revert to WITH-SLOTS after fixing the ...
    (comp.lang.lisp)