From: Slobodan Blazeski
Subject: Few Questions about CLOS?
Date: 
Message-ID: <1191447732.999351.225670@r29g2000hsg.googlegroups.com>
1.How to access all the slots in the class  without previous knowledge
what slots that class contains ? Something like this
(let (res)
  (iterate-over-slots (slot class)
   (push slot res))
  (nreverse res))
Which will collect values of all the slots in ANY class you pass at :
2. if I have  a class with slots x & y   , which already has some
instances , is it possible to modify class such that for example by
adding slot z all the existing instances will be modified to keep
their old values of x & y and just get plus another slot z?

From: Rainer Joswig
Subject: Re: Few Questions about CLOS?
Date: 
Message-ID: <joswig-C135A8.23495103102007@news-europe.giganews.com>
In article <························@r29g2000hsg.googlegroups.com>,
 Slobodan Blazeski <·················@gmail.com> wrote:

> 1.How to access all the slots in the class  without previous knowledge
> what slots that class contains ? Something like this
> (let (res)
>   (iterate-over-slots (slot class)
>    (push slot res))
>   (nreverse res))

That's usually an extension of the implementation. Often
the functionality will look like the one described
by the MOP (Meta Object Protocol).

See the functions CLASS-SLOTS and CLASS-DIRECT-SLOTS

> Which will collect values of all the slots in ANY class you pass at :

> 2. if I have  a class with slots x & y   , which already has some
> instances , is it possible to modify class such that for example by
> adding slot z all the existing instances will be modified to keep
> their old values of x & y and just get plus another slot z?

That's the default behavior for CLOS instance when the class
is modified. The instances are usually lazy modified.
From: Pascal Costanza
Subject: Re: Few Questions about CLOS?
Date: 
Message-ID: <5mikdeFdghpcU1@mid.individual.net>
Slobodan Blazeski wrote:
> 1.How to access all the slots in the class  without previous knowledge
> what slots that class contains ? Something like this
> (let (res)
>   (iterate-over-slots (slot class)
>    (push slot res))
>   (nreverse res))
> Which will collect values of all the slots in ANY class you pass at :

(loop for slot-definition in (class-slots (find-class class))
       collect (slot-definition-name slot-definition))

The functions class-slots and slot-definition-name are part of the CLOS 
MOP, but not part of ANSI Common Lisp. So you have to make sure that you 
get them from the right package (typically named CLOS, MOP, CLOS-MOP, or 
so). Or you can use the Closer to MOP compatibility layer (see footer of 
this posting).

> 2. if I have  a class with slots x & y   , which already has some
> instances , is it possible to modify class such that for example by
> adding slot z all the existing instances will be modified to keep
> their old values of x & y and just get plus another slot z?

See Section 4.3.6 in the HyperSpec.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Slobodan Blazeski
Subject: Re: Few Questions about CLOS?
Date: 
Message-ID: <1191489017.900838.35230@19g2000hsx.googlegroups.com>
On Oct 4, 12:46 am, Pascal Costanza <····@p-cos.net> wrote:
> Slobodan Blazeski wrote:
> > 1.How to access all the slots in the class  without previous knowledge
> > what slots that class contains ? Something like this
> > (let (res)
> >   (iterate-over-slots (slot class)
> >    (push slot res))
> >   (nreverse res))
> > Which will collect values of all the slots in ANY class you pass at :
>
> (loop for slot-definition in (class-slots (find-class class))
>        collect (slot-definition-name slot-definition))
>
> The functions class-slots and slot-definition-name are part of the CLOS
> MOP, but not part of ANSI Common Lisp. So you have to make sure that you
> get them from the right package (typically named CLOS, MOP, CLOS-MOP, or
> so). Or you can use the Closer to MOP compatibility layer (see footer of
> this posting).
>
> > 2. if I have  a class with slots x & y   , which already has some
> > instances , is it possible to modify class such that for example by
> > adding slot z all the existing instances will be modified to keep
> > their old values of x & y and just get plus another slot z?
>
> See Section 4.3.6 in the HyperSpec.
>
> Pascal
>
> --
> My website:http://p-cos.net
> Common Lisp Document Repository:http://cdr.eurolisp.org
> Closer to MOP & ContextL:http://common-lisp.net/project/closer/


Thanks to both of you .
Slobodan
From: Richard Szopa
Subject: Re: Few Questions about CLOS?
Date: 
Message-ID: <1191537041.324100.203020@r29g2000hsg.googlegroups.com>
On Oct 3, 11:42 pm, Slobodan Blazeski <·················@gmail.com>
wrote:
> 1.How to access all the slots in the class  without previous knowledge
> what slots that class contains ? Something like this
> (let (res)
>   (iterate-over-slots (slot class)
>    (push slot res))
>   (nreverse res))

I do a lot things like that (and more) in Submarine (http://common-
lisp.net/project/submarine) so I wrote some utils for MOP. Then I
realized I may want to use them in some other projects of mine, so I
put it into a separate library. Maybe you will find it helpful. You
can get it from
http://common-lisp.net/project/submarine/darcs/mop-utils/ If you use
it and find and fix something specially stupid, please don't hesitate
to send me patch.

> Which will collect values of all the slots in ANY class you pass at :
> 2. if I have  a class with slots x & y   , which already has some
> instances , is it possible to modify class such that for example by
> adding slot z all the existing instances will be modified to keep
> their old values of x & y and just get plus another slot z?

This is a default behavior in CLOS. Just look:

CL-USER> (defclass foo () ((x :initarg :x :accessor x-of)
(y :initarg :y :accessor y-of)))
#<STANDARD-CLASS FOO>
CL-USER> (setf a-foo (make-instance 'foo :x 1 :y 2))
#<FOO {A9BA171}>
CL-USER> (x-of a-foo)
1
CL-USER> (defclass foo () ((x :initarg :x :accessor x-of)
(y :initarg :y :accessor y-of) (z :initarg :x :accessor z-of)))
#<STANDARD-CLASS FOO>
CL-USER> a-foo
#<FOO {A9BA171}>
CL-USER> (x-of a-foo)
1

Bests,

    -- Richard