Can you help me with the following.
I have a object. I do not know neither the number of slots nor names of
the slots. Is there a way to access them? Should I try to find a class
of an object and then get from the class info about slots? How? Or is
there other way.
<Just in case> -- use of with-slots requires names of slots, which,
again, I do not know.</>
Thanks in advance.
On Sun, 26 Mar 2006 11:14:07 -0800, Mikalai wrote:
> Can you help me with the following.
> I have a object. I do not know neither the number of slots nor names of
> the slots. Is there a way to access them? Should I try to find a class
> of an object and then get from the class info about slots?
Yes.
> How?
(class-of <your-object-here>)
From then on, use the MOP. Either you need to read up the MOP-
documentation of your favorite implementation or you can use
Pascal's (the person, not the language) handy closer-mop code
that hides implementation quir^h^h details :-)
Cheers RalfD
> Or is
> there other way.
> <Just in case> -- use of with-slots requires names of slots, which,
> again, I do not know.</>
> Thanks in advance.
Mikalai <········@yahoo.com> wrote:
+---------------
| Can you help me with the following.
| I have a object. I do not know neither the number of slots
| nor names of the slots. Is there a way to access them?
+---------------
In addition to other answers you've gotten, if you're sitting
at a REPL don't forget the simpleminded use of DESCRIBE:
> (describe random-object-1)
#<Closure Over Function "LAMBDA (X)" {4893B8E1}> is function.
Arguments:
There are no arguments.
Its defined argument types are:
NIL
Its result type is:
LIST
On Sunday, 3/26/06 07:39:32 pm PST it was compiled from:
#(#'(LAMBDA # #))
Its closure environment is:
0: 56
1: 34
2: 12
> (describe random-object-2)
#<Stream for file "/u/rpw3/foo.log"> is a structure of type FD-STREAM.
IN-BUFFER: NIL.
IN-INDEX: 512.
IN: #<Function LISP::INPUT-CHARACTER {101BB3F9}>.
BIN: #<Function LISP::ILL-BIN {10060F41}>.
N-BIN: #<Function LISP::FD-STREAM-READ-N-BYTES {10169299}>.
OUT: #<Function LISP::ILL-OUT {1011D601}>.
BOUT: #<Function LISP::ILL-BOUT {1006E481}>.
SOUT: #<Function LISP::ILL-OUT {1011D601}>.
MISC: #<Function LISP::FD-STREAM-MISC-ROUTINE {100C3439}>.
NAME: "file \"/u/rpw3/foo.log\"".
FILE: "/u/rpw3/foo.log".
ORIGINAL: NIL.
DELETE-ORIGINAL: NIL.
ELEMENT-SIZE: 1.
ELEMENT-TYPE: CHARACTER.
FD: 6.
BUFFERING: :FULL.
CHAR-POS: NIL.
LISTEN: NIL.
UNREAD: NIL.
IBUF-SAP: #.(SYSTEM:INT-SAP #x28137000).
IBUF-LENGTH: 4096.
IBUF-HEAD: 0.
IBUF-TAIL: 0.
OBUF-SAP: NIL.
OBUF-LENGTH: NIL.
OBUF-TAIL: 0.
OUTPUT-LATER: NIL.
HANDLER: NIL.
TIMEOUT: NIL.
PATHNAME: #p"/u/rpw3/foo.log".
>
Yes, DESCRIBE is implementation-specific, and some implementations
will tell you more or less than others. But it's a useful tool
when doing interactive exploration.
Ditto INSPECT...
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
From: Pascal Costanza
Subject: Re: CLOS question -- getting info about object's slots
Date:
Message-ID: <48odecFkmiviU1@individual.net>
Mikalai wrote:
> Can you help me with the following.
> I have a object. I do not know neither the number of slots nor names of
> the slots. Is there a way to access them? Should I try to find a class
> of an object and then get from the class info about slots? How? Or is
> there other way.
> <Just in case> -- use of with-slots requires names of slots, which,
> again, I do not know.</>
> Thanks in advance.
You need the CLOS MOP for this.
(class-slots (class-of object)) gives you the slots defined for an
object as slot definition metaobjects.
(mapcar #'slot-definition-name (class-slots (class-of object))) gives
you the same, but as slot names that you can use, for example, in calls
to slot-value.
The CLOS MOP is not part of ANSI Common Lisp, so you need to use an
implementation-dependent package that contains the CLOS MOP definitions.
Pascal
--
3rd European Lisp Workshop
July 3-4 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
Pascal Costanza wrote:
> Mikalai wrote:
> > Can you help me with the following.
> > I have a object. I do not know neither the number of slots nor names of
> > the slots. Is there a way to access them? Should I try to find a class
> > of an object and then get from the class info about slots? How? Or is
> > there other way.
> > <Just in case> -- use of with-slots requires names of slots, which,
> > again, I do not know.</>
> > Thanks in advance.
>
> You need the CLOS MOP for this.
>
> (class-slots (class-of object)) gives you the slots defined for an
> object as slot definition metaobjects.
>
> (mapcar #'slot-definition-name (class-slots (class-of object))) gives
> you the same, but as slot names that you can use, for example, in calls
> to slot-value.
>
> The CLOS MOP is not part of ANSI Common Lisp, so you need to use an
> implementation-dependent package that contains the CLOS MOP definitions.
>
Thank you, this should help.