From: Timofei Shatrov
Subject: Property inheritance
Date: 
Message-ID: <42e3742f.16674575@news.readfreenews.net>
Problem:
Is it possible to make :initform to collect results of :initforms of
superclasses?

More elaborate:
Suppose I have a class with a slot that contains a list of flags for an
object of this class. There is a default set of flags that each newly
created object of that class gets (through :initform). Now I want to
create a more specialised classes with some additional flags to be set
by default. Basically I want to run initforms of flags slot for each
superclass before running initform for subclass and append their
results. It looks like generic functions can do the job but it's
impossible to run a method for an object which is not yet created! I
could write a macro on top of make-instance to run a method after
creating object, but maybe a more elegant solution exists?

-- 
|a\o/r|,-------------.,---------- Timofei Shatrov aka Grue ------------.
| m"a ||FC AMKAR PERM|| mail: grue at mail.ru  http://grue3.tripod.com |
|  k  ||  PWNZ J00   || Kingdom of Loathing: Grue3 lvl 18 Seal Clubber |
`-----'`-------------'`-------------------------------------------[4*72]

From: Larry Clapp
Subject: Re: Property inheritance
Date: 
Message-ID: <slrnde7cjc.k91.larry@theclapp.ddts.net>
On 2005-07-24, Timofei Shatrov <····@mail.ru> wrote:
> Problem:
> Is it possible to make :initform to collect results of :initforms of
> superclasses?
>
> More elaborate:
> Suppose I have a class with a slot that contains a list of flags for
> an object of this class. There is a default set of flags that each
> newly created object of that class gets (through :initform). Now I
> want to create a more specialised classes with some additional flags
> to be set by default. Basically I want to run initforms of flags
> slot for each superclass before running initform for subclass and
> append their results. It looks like generic functions can do the job
> but it's impossible to run a method for an object which is not yet
> created!

An :AFTER method on initialize-instance performs just that function.

I couldn't come up with an elegant solution that specifically uses
:initform, but this performs a similar function:

(defclass class1 ()
  (slot))

(defclass class2 (class1)
  ())

(defgeneric init-slot (class) 
  (:method-combination list 	; could also use append or nconc
   :most-specific-last))

(defmethod init-slot list ((c class1))
  (declare (ignore c))
  (format t "init-slot for class1 called~%")
  'a)

(defmethod init-slot list ((c class2))
  (declare (ignore c))
  (format t "init-slot for class2 called ~%")
  'b)

(defmethod initialize-instance :after ((this class1) &key)
  (setf (slot-value this 'slot) (init-slot this)))

CL-USER> (make-instance 'class1)
init-slot for class1 called
#<CLASS1 {5868822D}>

CL-USER> (make-instance 'class2)
init-slot for class1 called
init-slot for class2 called 
#<CLASS2 {5868903D}>

CL-USER> (describe *)
#<CLASS2 {5868903D}> is an instance of class #<STANDARD-CLASS CLASS2 {5840C565}>:
 The following slots have :INSTANCE allocation:
 SLOT    (A B)

CL-USER> (defclass class3 (class2)
  ())
#<STANDARD-CLASS CLASS3 {5868F5BD}>

CL-USER> (defmethod init-slot list ((c class3))
  (declare (ignore c))
  (format t "init-slot for class3 called ~%")
  'c)
; Compiling LAMBDA (PCL::.PV-CELL. PCL::.NEXT-METHOD-CALL. C): 
; Compiling Top-Level Form: 
#<STANDARD-METHOD INIT-SLOT LIST (CLASS3) {586FA935}>

CL-USER> (make-instance 'class3)
init-slot for class1 called
init-slot for class2 called 
init-slot for class3 called 
#<CLASS3 {58749A65}>

CL-USER> (describe *)
#<CLASS3 {58749A65}> is an instance of class #<STANDARD-CLASS CLASS3 {5868F5BD}>:
 The following slots have :INSTANCE allocation:
 SLOT    (A B C)

Hope this helps.

-- Larry
From: Timofei Shatrov
Subject: Re: Property inheritance
Date: 
Message-ID: <42e3cbdb.39121665@news.readfreenews.net>
On Sun, 24 Jul 2005 15:23:57 GMT, Larry Clapp <·····@theclapp.org> tried
to confuse everyone with this message:

>On 2005-07-24, Timofei Shatrov <····@mail.ru> wrote:
>> Problem:
>> Is it possible to make :initform to collect results of :initforms of
>> superclasses?
>>
>> More elaborate:
>> Suppose I have a class with a slot that contains a list of flags for
>> an object of this class. There is a default set of flags that each
>> newly created object of that class gets (through :initform). Now I
>> want to create a more specialised classes with some additional flags
>> to be set by default. Basically I want to run initforms of flags
>> slot for each superclass before running initform for subclass and
>> append their results. It looks like generic functions can do the job
>> but it's impossible to run a method for an object which is not yet
>> created!
>
>An :AFTER method on initialize-instance performs just that function.
>
>I couldn't come up with an elegant solution that specifically uses
>:initform, but this performs a similar function:
>

>
>(defmethod initialize-instance :after ((this class1) &key)
>  (setf (slot-value this 'slot) (init-slot this)))
>

Great, that does just what I wanted.

-- 
|a\o/r|,-------------.,---------- Timofei Shatrov aka Grue ------------.
| m"a ||FC AMKAR PERM|| mail: grue at mail.ru  http://grue3.tripod.com |
|  k  ||  PWNZ J00   || Kingdom of Loathing: Grue3 lvl 18 Seal Clubber |
`-----'`-------------'`-------------------------------------------[4*72]
From: Kenny Tilton
Subject: Re: Property inheritance
Date: 
Message-ID: <UN1Fe.2885$Na6.1470094@twister.nyc.rr.com>
Timofei Shatrov wrote:
> On Sun, 24 Jul 2005 15:23:57 GMT, Larry Clapp <·····@theclapp.org> tried
> to confuse everyone with this message:
> 
> 
>>On 2005-07-24, Timofei Shatrov <····@mail.ru> wrote:
>>
>>>Problem:
>>>Is it possible to make :initform to collect results of :initforms of
>>>superclasses?
>>>
>>>More elaborate:
>>>Suppose I have a class with a slot that contains a list of flags for
>>>an object of this class. There is a default set of flags that each
>>>newly created object of that class gets (through :initform). Now I
>>>want to create a more specialised classes with some additional flags
>>>to be set by default. Basically I want to run initforms of flags
>>>slot for each superclass before running initform for subclass and
>>>append their results. It looks like generic functions can do the job
>>>but it's impossible to run a method for an object which is not yet
>>>created!
>>
>>An :AFTER method on initialize-instance performs just that function.
>>
>>I couldn't come up with an elegant solution that specifically uses
>>:initform, but this performs a similar function:
>>
> 
> 
>>(defmethod initialize-instance :after ((this class1) &key)
>> (setf (slot-value this 'slot) (init-slot this)))
>>
> 
> 
> Great, that does just what I wanted.

In the future, please put your answers in the form of questions.

-- 
Kenny

Why Lisp? http://lisp.tech.coop/RtL%20Highlight%20Film

"I've wrestled with reality for 35 years, Doctor, and I'm happy to state 
I finally won out over it."
     Elwood P. Dowd, "Harvey", 1950
From: Timofei Shatrov
Subject: Re: Property inheritance
Date: 
Message-ID: <42e4ba0c.3121441@news.readfreenews.net>
On Mon, 25 Jul 2005 08:55:16 GMT, Kenny Tilton <·······@nyc.rr.com>
tried to confuse everyone with this message:

>
>In the future, please put your answers in the form of questions.
>

What?

-- 
|a\o/r|,-------------.,---------- Timofei Shatrov aka Grue ------------.
| m"a ||FC AMKAR PERM|| mail: grue at mail.ru  http://grue3.tripod.com |
|  k  ||  PWNZ J00   || Kingdom of Loathing: Grue3 lvl 18 Seal Clubber |
`-----'`-------------'`-------------------------------------------[4*72]
From: Kenny Tilton
Subject: Re: Property inheritance
Date: 
Message-ID: <i58Fe.2894$Na6.1498834@twister.nyc.rr.com>
Timofei Shatrov wrote:
> On Mon, 25 Jul 2005 08:55:16 GMT, Kenny Tilton <·······@nyc.rr.com>
> tried to confuse everyone with this message:
> 
> 
>>In the future, please put your answers in the form of questions.
>>
> 
> 
> What?
> 

Sorry, I misread and was being unnecessarily idiotic. Meanwhile...

...one of the accidental benefits of my Cells 
(http://common-lisp.net/project/cells/) package is that you can specify 
a form for an initarg, which form references other slots of the as yet 
unborn instance:

     (make-instance 'shape :height (c? (* 2 (width self))) :width 42)

This is a lot more fun than mucking with initialize-instance.

-- 
Kenny

Why Lisp? http://lisp.tech.coop/RtL%20Highlight%20Film

"I've wrestled with reality for 35 years, Doctor, and I'm happy to state 
I finally won out over it."
     Elwood P. Dowd, "Harvey", 1950
From: Ivan Boldyrev
Subject: Re: Property inheritance
Date: 
Message-ID: <0accr2-esq.ln1@ibhome.cgitftp.uiggm.nsc.ru>
On 9180 day of my life Timofei Shatrov wrote:
> It looks like generic functions can do the job but it's impossible
> to run a method for an object which is not yet created!

Common Lisp is funny language: see ALLOCATE-INSTANCE and
INITIALIZE-INSTANCE.

I don't quite understand what you want, but perhaps around-method of
INITIALIZE-INSTANCE or ALLOCATE-INSTANCE can aid.

Feel free to contact me in Russian; e-mail is valid.

-- 
Ivan Boldyrev

       Assembly of a Japanese bicycle requires greatest peace of spirit.