Re: Listing slots in CLOS
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Mon, 31 Mar 2008 00:52:09 +0200
vanekl wrote:
Rupert Swarbrick wrote:Hi,
Is there a function, which will return a list (or whatever) of the slots in a class that I've got an instance of? I'm working with a weird auto-
generated binding library, which I don't really understand... and I'd like to be able to eyeball the fields I can play with!
The only things I can find are slot-exists-p and slot-bound-p, which of course aren't quite what I'm after. If there's not a portable way to do this, I'm using sbcl.
Rupert
[211]> (asdf:oos 'asdf:load-op :closer-mop)
...
[212]> (defclass c1 () (a b))
#1=#<STANDARD-CLASS C1>
[213]> (defclass c2 (c1) (d e f))
#1=#<STANDARD-CLASS C2>
[214]> (closer-mop:compute-slots (find-class 'c2))
(#<CLOS:STANDARD-EFFECTIVE-SLOT-DEFINITION A #x2052E666>
#<CLOS:STANDARD-EFFECTIVE-SLOT-DEFINITION B #x2052E6A6>
#<CLOS:STANDARD-EFFECTIVE-SLOT-DEFINITION D #x2052E6E6>
#<CLOS:STANDARD-EFFECTIVE-SLOT-DEFINITION E #x2052E726>
#<CLOS:STANDARD-EFFECTIVE-SLOT-DEFINITION F #x2052E766>)
[217]> (dolist (slot (closer-mop:compute-slots (find-class 'c2)))
(print (closer-mop:slot-definition-name slot)))
A
B
D
E
F
NIL
Don't use compute-slots every time. Just use class-slots.
Compute-slots actually performs a computation (hence the name), to traverse the class hierarchy and determine all the effective slots for a class. This can be a relatively costly computation. Class-slots just looks up the recently computed set of slots.
That's a general rule of thumb: If you only want to look at what properties a class (or any metaobject) has, avoid the compute-xyz functions, but first check whether there are any readers for the information you're looking for.
Pascal
--
1st European Lisp Symposium (ELS'08)
http://prog.vub.ac.be/~pcostanza/els08/
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
.
- References:
- Listing slots in CLOS
- From: Rupert Swarbrick
- Re: Listing slots in CLOS
- From: vanekl
- Listing slots in CLOS
- Prev by Date: Re: indent for CL
- Next by Date: Re: indent for CL
- Previous by thread: Re: Listing slots in CLOS
- Next by thread: Re: Listing slots in CLOS
- Index(es):