[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Inspecting structures and clos objects

William James

6/8/2015 5:37:00 PM

Vebjorn Ljosa wrote:

> | 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/al....
>
> I don't think there's anything similar for structures.

Gauche Scheme:

(use gauche.record)

(define-record-type point-2d #t #t x y)

(define-record-type (point-3d point-2d) #t #t z)

(rtd-all-field-names (class-of (make-point-3d 4 6 8)))
===>
#(x y z)


(let1 point (make-point-3d 4 6 8)
(vector-map
(pa$ ~ point)
(rtd-all-field-names (class-of point))))
===>
#(4 6 8)


--
This is war, and our greatest enemy is the enemy within: the submissive,
apologetic, guilt-ridden, self-hating drone. The moment we manage to destroy
the enemy within, destroying the rest of our enemies will be a walk in the
park. http://www.kolumbus.fi/aquilon/londonsp...