From: Marc Mertens
Subject: Inspecting structures and clos objects
Date: 
Message-ID: <pan.2001.11.28.20.59.10.948053.2296@vt4.net>
Hello,

Given a struct or a clos object is there a way to get a list of the
values of the different slots in the struct or clos object without having
to know the accessor functions (of course given a instance of a object or
a struct, getting a list of the accessors will also answer my question).

Thanks a lot in advance.

Marc Mertens
········@vt4.net
············@azlan.com

From: Vebjorn Ljosa
Subject: Re: Inspecting structures and clos objects
Date: 
Message-ID: <cy3hereipmp.fsf@ljosa.com>
* Marc Mertens <········@vt4.net>
| 
| Given a struct or a clos object is there a way to get a list of the
| values of the different slots in the struct or clos object without having
| to know the accessor functions (of course given a instance of a object or
| a struct, getting a list of the accessors will also answer my question).

Not in standard Common Lisp.  But many implementations support (fully
or partly) the Metaobject Protocol (MOP), which gives you what you
need for objects.

(defclass foo ()
  ((slot1)))

(defclass bar (foo)
  ((slot2)))

(mapcar #'clos:slot-definition-name 
        (clos:compute-slots (class-of (make-instance 'bar))))
=> (SLOT1 SLOT2)

(You can then use the standard SLOT-VALUE to get the values.)

The Metaobject Protocol is described in the book _The Art of the
Metaobject Protocol_ by Gregor Kiczales et al.  The reference part,
chapters 5 and 6, are available on the web:
<URL:http://www.elwoodcorp.com/alu/mop/>.

I don't think there's anything similar for structures.

-- 
Vebjorn Ljosa
From: Pierre R. Mai
Subject: Re: Inspecting structures and clos objects
Date: 
Message-ID: <87wv0ajzf8.fsf@orion.bln.pmsf.de>
Vebjorn Ljosa <·····@ljosa.com> writes:

> * Marc Mertens <········@vt4.net>
> | 
> | Given a struct or a clos object is there a way to get a list of the
> | values of the different slots in the struct or clos object without having
> | to know the accessor functions (of course given a instance of a object or
> | a struct, getting a list of the accessors will also answer my question).
> 
> Not in standard Common Lisp.  But many implementations support (fully
> or partly) the Metaobject Protocol (MOP), which gives you what you
> need for objects.
> 
> (defclass foo ()
>   ((slot1)))
> 
> (defclass bar (foo)
>   ((slot2)))
> 
> (mapcar #'clos:slot-definition-name 
>         (clos:compute-slots (class-of (make-instance 'bar))))
> => (SLOT1 SLOT2)
> 
> (You can then use the standard SLOT-VALUE to get the values.)
> 
> The Metaobject Protocol is described in the book _The Art of the
> Metaobject Protocol_ by Gregor Kiczales et al.  The reference part,
> chapters 5 and 6, are available on the web:
> <URL:http://www.elwoodcorp.com/alu/mop/>.
> 
> I don't think there's anything similar for structures.

Many implementations that support the MOP, also support the obvious
extension of allowing slot-value for instances with a meta-class of
structure-class.

Fewer implementations also implement other parts of the MOP for
structure-class instances, like e.g. compute-slots.  The ones I know
about are PCL and CMUCL/PCL.

CLISP, which doesn't support the MOP[1], will allow you to use
slot-value on instances of instances of structure-class. Its
internal function clos::class-slots (together with
closs::slotdef-name) will work on instances of
structure-class, and clos::slot-names will work on instances of such
instances, e.g.

(defun all-values (instance)
  (mapcar #'(lambda (slot-name) (slot-value instance slot-name))
          (clos::slot-names instance)))

Regs, Pierre.

Footnotes: 
[1]   With its built-in CLOS.  You can load PCL as a replacement.

-- 
Pierre R. Mai <····@acm.org>                    http://www.pmsf.de/pmai/
 The most likely way for the world to be destroyed, most experts agree,
 is by accident. That's where we come in; we're computer professionals.
 We cause accidents.                           -- Nathaniel Borenstein
From: Kent M Pitman
Subject: Re: Inspecting structures and clos objects
Date: 
Message-ID: <sfwitbuipc2.fsf@shell01.TheWorld.com>
Marc Mertens <········@vt4.net> writes:

> Given a struct or a clos object is there a way to get a list of the
> values of the different slots in the struct or clos object without having
> to know the accessor functions (of course given a instance of a object or
> a struct, getting a list of the accessors will also answer my question).

Not without the MOP.

Just as well.  It's normally an abstraction violation anyway.  It's useful
for certain kinds of metaprogramming, and that's why it's properly part of
the MOP, which most implementations support.
From: Coby Beck
Subject: Re: Inspecting structures and clos objects
Date: 
Message-ID: <micN7.131847$Yb.34808328@typhoon.tampabay.rr.com>
"Marc Mertens" <········@vt4.net> wrote in message
········································@vt4.net...
> Hello,
>
> Given a struct or a clos object is there a way to get a list of the
> values of the different slots in the struct or clos object without having
> to know the accessor functions (of course given a instance of a object or
> a struct, getting a list of the accessors will also answer my question).
>
> Thanks a lot in advance.
>

If it is just for your debugging, check DESCRIBE or try this (done in
LispWorks):

(loop for slot-name in (mapcar #'slot-definition-name (class-slots (class-of
obj)))
                   collect (list slot-name
                                 (if (slot-boundp obj slot-name)
                                     (slot-value obj slot-name)
                                   "unbound")))

--
Coby
(remove #\space "coby . beck @ opentechgroup . com")
From: Petter Gustad
Subject: Re: Inspecting structures and clos objects
Date: 
Message-ID: <873d2vwc7y.fsf@filestore.home.gustad.com>
Marc Mertens <········@vt4.net> writes:

> Hello,
> 
> Given a struct or a clos object is there a way to get a list of the
> values of the different slots in the struct or clos object without having
> to know the accessor functions (of course given a instance of a object or
> a struct, getting a list of the accessors will also answer my question).

If this is for debugging purposes:

(describe object)

Petter
-- 
________________________________________________________________________
Petter Gustad   8'h2B | (~8'h2B) - Hamlet in Verilog   http://gustad.com