Re: Dynamically defining slots



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
.



Relevant Pages

  • Re: why does the mop handle default-initargs as persistent,
    ... > why does the mop handle default-initargs as persistent, ... > why properties like default-initargs were treated as persistent across ... defclass is where implementors have differed. ... this simple sentence from the spec settles the ...
    (comp.lang.lisp)
  • Re: MOP question: custom slot options that behave like :INITFORM
    ... >> macro to get what I want. ... >> desired behaviour with the MOP alone? ... same as defclass, like this one currently is - if you rename my-defclass ... Other aspects of the specific nature of this macro could be similarly ...
    (comp.lang.lisp)
  • Re: defining a class dynamically
    ... (defun register-new () ... You'll want to investigate using the MOP for this. ... you can macroexpand a sample defclass form ...
    (comp.lang.lisp)
  • Re: why does the mop handle default-initargs as persistent,
    ... Classes of your imagined metaclass ... It is unfortunate that your example jumps from defclass to ensure-class, ... desired for some new:my-option option to defclass. ...
    (comp.lang.lisp)
  • Re: why does the mop handle default-initargs as persistent,
    ... > the spec for defclass. ... > insert defclass options or labor to make defclass and ensure-class ... for:default-initargs is not an option because we want (ensure-class ... macro learn about metaclass options and their default values in order to ...
    (comp.lang.lisp)