Re: Dynamically defining slots
- From: tar@xxxxxxxxxxxxx (Thomas A. Russ)
- Date: 27 Aug 2008 13:58:19 -0700
Mark Carter <me@xxxxxxxxxxx> writes:
Is there a way to add slots to a class dynamically, or must they all be
defined in one go during defclass?
This actually turns out to be a more complicated question to answer than
you might imagine. The short answer, though, is that you have to do all
of the slots in a single definition. Although you can later use a
different definition.
1. just using ANSI Common Lisp:
The slots need to be defined in a DEFCLASS form. But you can later use
another DEFCLASS to redefine the class. If you add slots to the class,
the existing instances of that class will be updated with the new slots.
You can even control what is done with new (and old slots that go away)
using the standard method UPDATE-INSTANCE-FOR-REDEFINED-CLASS.
Now, unfortunately, there isn't any ANSI standard functional interface
to redefining a class. So you will end up having to either use DEFCLASS
directly or else using (EVAL (DEFCLASS ...)) to effect the change.
2. using the Metaobject Protocol (MOP)
See http://cdr.eurolisp.org/document/1/spec.pdf
This is a separate standard that many Common Lisp implementaitons
support. It provides access to a protocol for examining and changing
class and method definitions. In the case of getting a new class
definition you would use ENSURE-CLASS. You would need to recreate all
of the pieces of the class definition and then augment it with the slot
that you wish to add.
But all of the information you need is available through the MOP
accessors, but you would need to assemble all of the pieces, and then
format the information into the appropriate list structure. So, it
would then be possible to write your own ADD-SLOT macro or function that
will take the requisite information and construct an augmented class
definition to use with ENSURE-CLASS.
--
Thomas A. Russ, USC/Information Sciences Institute
.
- Prev by Date: Re: 419 spammer harvested address
- Next by Date: Re: What do you LISPers think of Haskell?
- Previous by thread: Re: Dynamically defining slots
- Next by thread: Re: 419 spammer harvested address
- Index(es):
Relevant Pages
|