Re: Dynamically defining slots



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



Relevant Pages

  • Re: Comparing Lisp conditions to Java Exceptions
    ... post came across very critical of Common Lisp (that is, ... > to be explicit on this and there was no money to do that project. ... > when you don't see something in the formal standard written and ... No reason why I won't try to do so in the future. ...
    (comp.lang.lisp)
  • Re: How Common Lisp sucks
    ... in the Common Lisp community. ... libraries that work across implementations that provide de facto ... This is one of the very few instances of behavior outside the standard ... to the language at all. ...
    (comp.lang.lisp)
  • Re: Qi Seems Great
    ... Common Lisp will simply trounce anything else in the long ... but having third party libraries to support the new ... ideas come along doesn't mean that the language itself will grow. ... Having in mind that the CL standard is rather old, ...
    (comp.lang.lisp)
  • Re: Running Standalone Lisp Programs
    ... On platforms without a command line ... into the ANSI Common Lisp standard is a very problematic venture. ... > for not including file system operations in the CL standard. ...
    (comp.lang.lisp)
  • Re: scheme seems neater
    ... people are not interested in extending Common Lisp? ... ANSI CL standard is, for example, a system definition facility. ... the fact that a hygienic macro system doesn't exist for Common ... implementations of syntax-rules bug-free? ...
    (comp.lang.lisp)