From: Mark Carter
Subject: Dynamically defining slots
Date: 
Message-ID: <48b599dd$0$2930$fa0fcedb@news.zen.co.uk>
Is there a way to add slots to a class dynamically, or must they all be 
defined in one go during defclass?

I've had a look at the permuted index for words containing 'slot', but I 
can't seem to find what I want.

From: Thomas A. Russ
Subject: Re: Dynamically defining slots
Date: 
Message-ID: <ymimyiydvkk.fsf@blackcat.isi.edu>
Mark Carter <··@privacy.net> 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
From: John Thingstad
Subject: Re: Dynamically defining slots
Date: 
Message-ID: <op.ugj83jugut4oq5@pandora.alfanett.no>
P� Wed, 27 Aug 2008 20:15:56 +0200, skrev Mark Carter <··@privacy.net>:

> Is there a way to add slots to a class dynamically, or must they all be  
> defined in one go during defclass?
>

You can add slots dynamically.
One way is just to add slots to a existing defclass and just recompile it.
Changes are propegated to all instances of the class. They are changed  
lazily before access.
There is a function update-instance-for-redefined-class which you can  
customize to change default behaviour.

> I've had a look at the permuted index for words containing 'slot', but I  
> can't seem to find what I want.

If you want to get serious about CLOS you might want to invest in "Object  
oriented programming in Common Lisp" by Sonya E. Keene.
I am currently looking at chapter 8 "Redefining CLOS elements"

To add attributes dynamically without redefining the definition I think  
you need to use the Meta Object Protocal (MOP)
You should probaly read up on MOP before attempting this.

--------------
John Thingstad
From: Rainer Joswig
Subject: Re: Dynamically defining slots
Date: 
Message-ID: <joswig-5E969E.22095027082008@news-europe.giganews.com>
In article <························@news.zen.co.uk>,
 Mark Carter <··@privacy.net> wrote:

> Is there a way to add slots to a class dynamically, or must they all be 
> defined in one go during defclass?
> 
> I've had a look at the permuted index for words containing 'slot', but I 
> can't seem to find what I want.

If you search this newsgroup you will find several discussions
with solutions.

-- 
http://lispm.dyndns.org/
From: Pascal Costanza
Subject: Re: Dynamically defining slots
Date: 
Message-ID: <6hlqnkFkrq6cU1@mid.individual.net>
Mark Carter wrote:
> Is there a way to add slots to a class dynamically, or must they all be 
> defined in one go during defclass?
> 
> I've had a look at the permuted index for words containing 'slot', but I 
> can't seem to find what I want.

(1) Change the defclass form and recompile.

(2) Use the CLOS MOP and perform a reinitialize-instance on a class 
metaobject to add the slots you need. However, this is quite complicated 
and, if done wrong, can render you runtime unusable. I don't recommend 
doing this. (AspectL provides some layer around this which makes this a 
bit easier, but I would also not recommend using that.)

(3) Use the CLOS MOP and define your own metaclass as a subclass of 
standard-class which allows you to do that. This is actually one of the 
standard examples for the use of the CLOS MOP.

(4) Hook into the slot-missing protocol. That's part of ANSI Common 
Lisp: You can define a method on slot-missing that looks up undefined 
slots somewhere else, for example in a hash table associated with the 
object. The advantage of (4) is that it uses only standard features of 
Common Lisp, but it's a bit hackish. (3) is a bit "cleaner" but not 
standard - however, it is quite well supported all the major Common Lisp 
implementations.

(5) Use ContextL. Use define-layered-class instead of defclass. If you 
want to add new slots to an existing layered class, define a new layer 
and add the slots to that class in that new layer. This way you don't 
have to touch the original (layered) class definition. ContextL is quite 
stable by now and in relatively wide use, so this should be reliable.

Depending on context, except for (2) all of them are fine. Personally, I 
would lean towards (4) and (5), and ofc (1) for the very simple cases.


I hope this helps,
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/