Re: Dynamically defining slots
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Wed, 27 Aug 2008 22:11:33 +0200
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/
.
- Prev by Date: Re: Dynamically defining slots
- Next by Date: Re: What do you LISPers think of Haskell?
- Previous by thread: Re: Dynamically defining slots
- Next by thread: Re: Dynamically defining slots
- Index(es):
Relevant Pages
|