From: Kevin Mayall
Subject: Listing the slots of a class?
Date: 
Message-ID: <336E0D09.36ED@uwaterloo.ca>
Two questions:

1. Is there a CLtL2 way to list the slots defined for a class?  In
Allegro CL for Win, they list the class-slots function in their Common
Lisp package, but I don't see it in Steele.

2. I'm currently using the class-slots function mentioned above, but I'm
having trouble listing the slots BEFORE I create an instance.  Can
anyone enlighten me as to what is going on here?  Here's some code and
results:

> (defclass Foobar ()
    ((slot1 :initarg :slot1 :accessor slot1 :initform NIL
       :documentation "the docstring for slot1"))
   (:documentation "the docstring for foobar"))

#<STANDARD-CLASS FOOBAR #xE84528>

> (class-slots (find-class 'foobar))
;; Error: Slot ACL::SLOTS unbound in instance #<STANDARD-CLASS FOOBAR
#xE84528> of class STANDARD-CLASS.

;; Returning to Top Level

> (setq a-foobar-instance (make-instance 'foobar :slot1 '(a b c)))
#<FOOBAR #xE68CBC>

> (slot1 a-foobar-instance)
(A B C)

> (class-slots (find-class 'foobar))      ; the second time
(#<STANDARD-EFFECTIVE-SLOT-DEFINITION SLOT1 #xE688B0>)

>

From: Heiko Kirschke
Subject: Re: Listing the slots of a class?
Date: 
Message-ID: <u207l2hlu.fsf@poet.de>
Kevin Mayall <·······@uwaterloo.ca> writes:

> 1. Is there a CLtL2 way to list the slots defined for a class?  In

Yes.

> Allegro CL for Win, they list the class-slots function in their Common
> Lisp package, but I don't see it in Steele.

class-slots is a part of the Metaobject Protocol (MOP) and described
in Kiczales et al.: The Art of the Metaobject Protocol. (BTW: Has
someone a newer reference w.r.t. AMOP? Is it ANSI now or is it not?)
There are some overlappings between Steele and Kiczales, but
class-slots doesn't belong to it.

> 2. I'm currently using the class-slots function mentioned above, but I'm
> having trouble listing the slots BEFORE I create an instance.  Can
> anyone enlighten me as to what is going on here?

When you define a class in CLOS, i.e. evaluate/load a
defclass-statement, the class is not yet a full-featured class, since
there might be missing information for the class to be complete. One
example is if you specify a not yet existing class in the list of
superclasses of another class; for the missing class, an instance of
forward-referenced-class is generated (this is a kind of a placeholder
object assuring that the class won't loose its identity when defined
later). The class with missing information(s) gets a full-featured
class at the latest possible moment, and one of these is when you
create an instance of the class. The turning into a full-featured
class is called finalization in CLOS. There are two (generic?)
functions having to do with finalization: The first one
(class-finalized-p) checks if a class is already finalized
(i.e. `full-featured'), the other one (finalize-inheritance) forces a
class finalization. So, to solve your problem you've to make sure that
your class is finalized:

(defun finalized-class-slots (the-class)
  (unless (class-finalized-p the-class)
     (finalize-inheritance the-class))
  (class-slots the-class))

Please note that the finalize-inheritance call might fail when the
class declaration is incomplete, e.g. if it references a not yet
defined superclass.

Viele Gruesse, Heiko

(PS: Hi #\Erik, was this exactly enough ? ;-)
--
Heiko Kirschke             EMail: ··············@poet.de
POET Software GmbH         Web:   http://www.poet.de/
Fossredder 12              Tel:   +49 40 60990-263
D-22359 Hamburg            Fax:   +49 40 60990-115
From: Francis Leboutte
Subject: Re: Listing the slots of a class?
Date: 
Message-ID: <33732cdd.2167927@nntp.z02.glo.be>
Heiko Kirschke <··············@poet.de> wrote:

>Kevin Mayall <·······@uwaterloo.ca> writes:
>
>> 1. Is there a CLtL2 way to list the slots defined for a class?  In
>
>Yes.
>
>> Allegro CL for Win, they list the class-slots function in their Common
>> Lisp package, but I don't see it in Steele.
>
>class-slots is a part of the Metaobject Protocol (MOP) and described
>in Kiczales et al.: The Art of the Metaobject Protocol. (BTW: Has
>someone a newer reference w.r.t. AMOP? Is it ANSI now or is it not?)

You maybe know this excellent introduction to the CLOS MOP:

User-Level Language Crafting: Introducing the CLOS Metaobject Protocol (A.
Paepcke), page 65 in Object Oriented Programming - The CLOS Perspective.
Edited by A.P. (the MIT Press).

--
Francis Leboutte, Algorithme, Rue de la Charrette 141, 4130 Tilff, Belgium
····@glo.be      http://user.glo.be/~algo     t&fax: +32-(0)4-3883528
From: Stephen Glass
Subject: Re: Listing the slots of a class?
Date: 
Message-ID: <336F95CA.2AC6@compuserve.com>
Francis Leboutte wrote:
> 
[snip]
> 
> You maybe know this excellent introduction to the CLOS MOP:
> 
> User-Level Language Crafting: Introducing the CLOS Metaobject Protocol (A.
> Paepcke), page 65 in Object Oriented Programming - The CLOS Perspective.
> Edited by A.P. (the MIT Press).
> 
> --
> Francis Leboutte, Algorithme, Rue de la Charrette 141, 4130 Tilff, Belgium
> ····@glo.be      http://user.glo.be/~algo     t&fax: +32-(0)4-3883528

Sadly, this book is now out of print. If anyone out there
knows of a (UK) source then please let me know.

Steve

······@acm.org
From: Thomas A. Russ
Subject: Re: Listing the slots of a class?
Date: 
Message-ID: <ymin2q839dn.fsf@indra.isi.edu>
In article <·············@uwaterloo.ca> Kevin Mayall <·······@uwaterloo.ca> writes:

 > From: Kevin Mayall <·······@uwaterloo.ca>
 > Date: Mon, 5 May 1997 16:38:33 GMT
 > Two questions:
 > 
 > 1. Is there a CLtL2 way to list the slots defined for a class?  In
 > Allegro CL for Win, they list the class-slots function in their Common
 > Lisp package, but I don't see it in Steele.

Not really a CLtL2 way, but the Meta-Object Protocol (MOP) includes
functions for finding slots of classes.

 > 2. I'm currently using the class-slots function mentioned above, but I'm
 > having trouble listing the slots BEFORE I create an instance.  Can
 > anyone enlighten me as to what is going on here?  Here's some code and
 > results:

I suspect that it has something to do with the class not being
"finalized" until an instance is created.  Delaying finalization is an
efficiency measure that can help avoid the need to keep redefining class
structures as more CLOS classes are defined and the class inheritance
hierarchy changes.  The idea is to wait until you need the information
before computing the class layout.  It would appear that the MOP
function doesn't check to make sure the class is finalized before trying
to access the internal representation.  Perhaps someone from Franz could
expand on this.

-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Vassili Bykov
Subject: Re: Listing the slots of a class?
Date: 
Message-ID: <01bc5a33$ba0430c0$752d54c7@vbykov.hip.cam.org>
Kevin Mayall <·······@uwaterloo.ca> wrote in article
<·············@uwaterloo.ca>...
> Two questions:
> 
> 1. Is there a CLtL2 way to list the slots defined for a class?  In
> Allegro CL for Win, they list the class-slots function in their Common
> Lisp package, but I don't see it in Steele.

It belongs in MOP (metaobject protocol) which is not described in CLtL.

> 2. I'm currently using the class-slots function mentioned above, but I'm
> having trouble listing the slots BEFORE I create an instance.  Can
> anyone enlighten me as to what is going on here?  Here's some code and
> results:

Class initialization in CLOS is a two-step process: 1) initialization
itself which sets the immediate class attributes such as the direct slot
list; and 2) finalization: computing the class precedence list, effective
slot list, and initargs.  Finalization is a step separate from
initialization to allow forward references to superclasses.  For standard
classes it is performed at some moment after all the class's superclasses
have been defined, but before the first instance is created.

> > (defclass Foobar ()
>     ((slot1 :initarg :slot1 :accessor slot1 :initform NIL
>        :documentation "the docstring for slot1"))
>    (:documentation "the docstring for foobar"))
> 
> #<STANDARD-CLASS FOOBAR #xE84528>

At this point the class metaobject is created and initialized. It *might*
be finalized by now since all the superclasses are initialized. Then again,
it might not be since the first instance has not yet been created.

> > (class-slots (find-class 'foobar))
> ;; Error: Slot ACL::SLOTS unbound in instance #<STANDARD-CLASS FOOBAR
> #xE84528> of class STANDARD-CLASS.

Apparently, it has not yet been finalized.

> > (setq a-foobar-instance (make-instance 'foobar :slot1 '(a b c)))
> #<FOOBAR #xE68CBC>

Sometime before the instance was created, the class definitely was
finalized...

> > (class-slots (find-class 'foobar))      ; the second time
> (#<STANDARD-EFFECTIVE-SLOT-DEFINITION SLOT1 #xE688B0>)

...and now we can get the effective slots.

Finalization is performed by a generic function FINALIZE-INHERITANCE.

--Vassili