Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Mirko.Vuko » Sat, 11 Oct 2008 02:24:34


I suspect the answer is no, and in which case I will revisit my
concept. But here is the story.

I have a base class that initializes storage. One of its subclasses
needs extra padding around the storage. The base classes initializes
the padding to zero, and the subclasses ought to change that. Is
there some way to specify this within the standard CLOS machinery?

Here is the example code:

(in-package :cl-user)

(defclass base ()
((dim
:initform 12
:accessor dim)
(padding
:initform 0
:accessor padding
:initarg :padding
:documentation "Padding is necessary for some higher order
interpolations")
(storage
:initform nil
:accessor storage)))
(defmethod initialize-instance :after ((this base) &key)
(format t "Initializing base ~a~%" this)
(setf (storage this) (make-array (+ (dim this)
(padding this))
:initial-element 0d0)))

(defclass sub1a (base)
((dummy
:initform 2d0
:accessor dummy))
(:documentation "This will implement a higher order interpolations
that needs padding")

;; I would like to modify padding here.
(defmethod initialize-instance :after ((this sub1a) &key)
(format t "Initializing sub ~a~%" this)
(setf (padding this) 2))


To get what I want, I need to do
but I would prefer to do just
This, of course does not work, because first the base's initialize-
instance method is called, which sort-of makes sense.

The first approach looks kind of kludgy, even though Keene (yep, I
went to the library and got the CLOS book) suggests using constructors
to hide the ugly make-instance details.

In real world, the base class is a an "abstract/virtual" (not sure of
the difference) class for a differential equation solver, while the
(layered) subclasses are particular instances of the solvers, some of
which build on top of others. The base class provides the basic
storage, and methods for storing, accessing, and numerical
computations.

Thanks,

Mirko
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Rainer Jos » Sat, 11 Oct 2008 03:25:00

n article
< XXXX@XXXXX.COM >,
XXXX@XXXXX.COM wrote:


Sure. Why not. Remember that all :after methods are running.

CL-USER 26 > (defclass c1 () (a))
#<STANDARD-CLASS C1 200AFF37>

CL-USER 27 > (defclass c2 (c1) ())
#<STANDARD-CLASS C2 200AE0B7>

CL-USER 28 > (defmethod initialize-instance :after ((object c1) &key) (setf (slot-value object 'a) 'c1-value))
#<STANDARD-METHOD INITIALIZE-INSTANCE (:AFTER) (C1) 200964C3>

CL-USER 29 > (defmethod initialize-instance :after ((object c2) &key) (setf (slot-value object 'a) 'c2-value))
#<STANDARD-METHOD INITIALIZE-INSTANCE (:AFTER) (C2) 21E2D45F>

CL-USER 30 > (describe (make-instance 'c2))

#<C2 21A36597> is a C2
A C2-VALUE


The slot is set twice. first to c1-value and then to c2-value.
First the :after method for C1 is running, then the :after method
for C2.


But then the :after methods are running. The :after methods
from the subclass is running later.

Similar, all the :before methods are running. The :before
methods from the subclasses are running first:

For a method like initialize-instance with an object of class C2:

:after c2
:after c1
primary c2 -> call-next-method -> primary c1
:before c1
:after c2

The value of the primary method for c2 is returned as the value
for the generic function call.

(there are also :around methods)


Also remember this:

CL-USER 33 > (defclass ce1 ()
((a :initform 'ce1-value)))
#<STANDARD-CLASS CE1 2009B607>

CL-USER 34 > (describe (make-instance 'ce1))

#<CE1 20099607> is a CE1
A CE1-VALUE

CL-USER 35 > (defclass ce2 ()
((a :initform 'ce2-value)))
#<STANDARD-CLASS CE2 200A60D7>

CL-USER 36 > (describe (make-instance 'ce2))

#<CE2 200DEB8F> is a CE2
A CE2-VALUE

Slots with the same name are merged. CE2 has only
one slot named A. The initform for CE2 will be used
when you make an instance of CE2.


--
http://lispm.dyndns.org/

 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Mirko.Vuko » Sat, 11 Oct 2008 03:54:46

n Oct 9, 2:25m, Rainer Joswig < XXXX@XXXXX.COM > wrote:
> > pa>d>ng
> > :ini>f>rm 0
> > :accesso> >adding
> > :inita>g>:padding
> > :documentation "Padding is necessary for some>h>gher order
> > int>r>olations")
> > > > :initform nil
> > :>c>essor storage)))
> > (defmethod initialize-instance :after>(>this base) &key)
> > (format t "Initializ>n> base ~a~%" this)
> > (setf (storage this) (mak>->rray (+ (dim this)
> > > > padding this))
> > > gt;gt;:>n>tial-elemen> >d0)))
>
> > (def>l>ss sub1a (base)
> > gt;(>dummy
> > :initform 2d0
> > :accessor dummy))
> > :docum>n>ation "This will implem>nt>a>higher order interpolations
> > that need> >adding")
>
> > ;; I would like to modify padding here.
> > >defmethod initialize-instance :after ((this>s>b1a) &key)
> > (format t >In>tializing sub ~a~%" this)
> > (setf (padding this) 2))
>
>>Su>e. Why not. >emember that all :after>methods are running<
>
> CL>US>R >6 > (defclas> c1 () (a))
> #< >
> CL->SE> 2> > (defclass>c2 (c1) ())
> #
>
> CL-USER 28 > (defmethod initialize-instance :afte> ((object c1)<&key) (setf (slot-value object 'a) 'c1-value)) >> >
>
> CL-USER 29 > (defmethod initialize-instance :afte> ((object c2)<&key) (setf (slot-value object 'a) 'c2-value)) >> >
>
> >L<USER 30 > (describe (make>in>tance 'c2))
>
> # is a C2
> A 2-VALUE
>
>>The slot is set twice. first to c1-value and then to c2-value.
> Fi>st the :af>er>me>ho> >or C1 is running, then the :after m>t>o>
> for C2.
>
>
>
> > To get wha> > want, I need to do
> > > (make>i>s>ance 'sub1a :padding 2)
> > > (make-instance 'sub1a)
> > This> >f course does not work, because first the base's initial>ze>
> > instance method is called, which sort-of makes sense.
>> > But then the :after methods are run>in>. The :after methods
> from the subclass is running later. >>
> Similar, all the :before methods are running> T>e :before
> methods from the subclasses are running first:
>
> Fo> a>method like i>itialize-insta>ce with an object>of class C2:
>
> > :after c2
>>after c1 >> rim>ry>c2 -> call-next-method -> primary c1
> before c1
> :afte> c2
>
> The value of the primary m>th>d for c2 is returned as the value
>
> (there are also :around methods)
> >>> >
> > The first approach looks kind of kludgy, even though Keene (yep, > >> > went to the library and got the CLOS b>ok> >uggests using constructors
> > to hide the ugly make-instance details. >>> > > In real world, the base class is a an "abstract/virtual" (not sur> >f
> > the difference) class for a differential equation solver, while t>e> > > (layered) subclasses are particular instances of the solvers, >o>e of
> > which build on top of others. he base class provi>e> the basic
> > >to>age, and methods for s>or>ng, accessin>, and numerical
> > computations.
>
> Also remember th>s:
>
> CL-USER 33 > (def<las>
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Rainer Jos » Sat, 11 Oct 2008 04:14:14

n article < XXXX@XXXXX.COM >,
Rainer Joswig < XXXX@XXXXX.COM > wrote:


Wow, I meant:


Not sure what I was thinking of...



--
http://lispm.dyndns.org/
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Mirko.Vuko » Sat, 11 Oct 2008 04:52:41

n Oct 9, 3:14m, Rainer Joswig < XXXX@XXXXX.COM > wrote:
> > > :ini>f>r> 0
> > > :accesso> >a>ding
> > > :inita>g>:>adding
> > > :documentation "Padding is necessary for some>h>g>er order
> > > int>r>o>ations")
> > > > storage
> > > > > >initform nil
> > > :>c>e>sor storage)))
> > > (defmethod initialize-instance :after>(>t>is base) &key)
> > > (format t "Initializ>n> >ase ~a~%" this)
> > > (setf (storage this) (mak>->r>ay (+ (dim this)
> > > > > > (padding this))
> > > > gt;gt;gt;gt;i>i>ial-element>0>0>))
>
> > > (defc>a>s>sub1a (base)
> > > >(>u>my
> > > :initform 2d0
> > > :accessor dummy))
> > > :do>u>e>tation "This will imple>en> > >igher order interpolations
> > > that nee>s>p>dding")
>
> > > ;; I would like to modify padding here.
>>>>>>(defmethod initialize-instance :after ((thi> >u>1a) &key)
> > > (format t>"I>i>ializing sub ~a~%" this)
> > > (setf (padding this) 2))
>
>
< > C>-U>ER>2> > (defclass>c1 () (a))
> > #
>
><> CL>US>R >7>> (defclass >2 (c1) ())
> > #
>
> > CL-USER 28 > (defmethod initialize-instance :a>t>r ((object c1) &ke<) (setf (slot-value object 'a) 'c1-value))> >>> ><>TANDARD-METH>D INITIALIZE-INSTANCE (:AFTER) (C1) 200964C3>
>
> > CL-USER 29 > (defmethod initialize-instance :a>t>r ((object c2) &ke<) (setf (slot-value object 'a) 'c2-value))> >>> ><>TANDARD-METH>D INITIALIZE-INSTANCE (:AFTER) (C>) >1>2D45F>
>
> >>CL-USER 3<>>>(describe (make->ns>a>ce 'c2))
>
> > # is a C2
> > A 2-VALUE
>
> >>F>rst the :a>te> >e>hod for C1 is running, then the :af>e> >e>hod
> > for C2.
>
> > > To get wh>t>I>want, I need to do
> > > > (mak>->n>t>nce 'sub1a :padding 2)
>>>>>>but I would prefer to do just
> > > > (make-instance 'sub1a)
> > > T>i>,>of course does not work, because first the base's initia>iz>-> > > > instance method is called, which sort-of makes sense.
> > from the subclass is running later.> > > Similar, all the :before methods are runnin>. >h> :before
> > methods from the subclasses are running first:
>
> >>Fo> > method like >n>tialize-instan>e>with an object of>class C2:
>
> > gt;:after c2
> > > after c1
> > :after c2 >>
> Wow, I meant: >>
> > :bef>r> c2
> > gt;b>fore c1
> > > gt;rimary c2 -> call-next-method -> >rimary c1
> > after c1
> > :after c2
>
> Not sure what I was thinking of...
Whatever you were thinking of was pretty good, as far as I'm
concerned :-)
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Mirko.Vuko » Sat, 11 Oct 2008 10:49:30

n Oct 9, 4:05m, Kenny < XXXX@XXXXX.COM > wrote:
> > pa>d>ng
> > :ini>f>rm 0
> > :accesso> >adding
> > :inita>g>:padding
> > :documentation "Padding is necessary for some>h>gher order
> > int>r>olations")
> > > > :initform nil
> > :>c>essor storage)))
> > (defmethod initialize-instance :after>(>this base) &key)
> > (format t "Initializ>n> base ~a~%" this)
> > (setf (storage this) (mak>->rray (+ (dim this)
> > > > padding this))
> > > gt;gt;:>n>tial-elemen> >d0)))
>
> > (def>l>ss sub1a (base)
> > gt;(>dummy
> > :initform 2d0
> > :accessor dummy))
> > :docum>n>ation "This will implem>nt>a>higher order interpolations
> > that need> >adding")
>
> > ;; I would like to modify padding here.
> > >defmethod initialize-instance :after ((this>s>b1a) &key)
> > (format t >In>t>alizing sub ~a~%" this)
> > (set> (>a>>ing this) 2))
>
> > To get what I>wa>t> I need to do
>
> >>(make-inst>nc> >>ub1a :padding 2)
>
> >>bu> > would prefer to do just
>
> >>(mak>-i>stance 'sub1a)
>
> > This, of course does not work, ...
>
> I wish y>u had proved that with repl output...oh, christ, you are not
> talking abo>t padding, you are talking about the storage. Which takes me
> back to >our use of "This" which in turn takes me back to high school
> geometry >nd Mr. Wiseman who always s>id>">here's no room for pronouns
> in geome>r>, Mr. Tilton.
>
> > because first the base's initializ>- >> > instance method is called, which sort-of makes sense.
>
> That>One will be along shortly to recommend either a metaclass or
> AspectL,>I will recommend Cells which always does things In the Right
> Order(tm).>And happily saves you from having to write i-i methods, that
> >am> is too long and I have carpal tunnel, dammit! I digress.
>
> A humbler ">ee, let's do everything with CLOS!" solution would be to use
> a> :around method to set up the storage slot, do>ng>so /after/
> call-next-method to get the desired effect.
>
> But now we >ave the CLOS tail wagging the dog, which I am afraid is you.
> Wh>t happens when things get more complicated and you run out of
>
> Inter>stingly, Cells was invented precisely to solve a problem with
> order of>calculation -- one where slots needed the value of slots from
> other i>stances as well as the same -- not to do reactive programming
> (the ha>diest name for this particular pet trick). Reactive just fell
> out o> the tree, which was the second sign to me that Cells was way
> cool. The>first was how it trivially dispatched the daunting problem of
> getting complex gui geometry calculated in the right order.

Hmm. >I >il> n>t>change the code for now, but I'll keep it in mind.
>
>
>
> > The >i>st approach looks kind of kludgy, even though Keene (yep, I
> > went to >h> library and got the CLOS book) suggests u>in> constructors
> > to hide the ugly make-instance details.
>
> She was>si>pl> w>o>g. That breaks the usefulness of generic dispatch.
>
>
>
> > In real>w>rld, the base class is a an "abstract/virtual" (not sure of
> > the d>f>erence) class for a differential equation solver, while the
> > (layere>)>subclasses are particular instan
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Kenn » Sat, 11 Oct 2008 14:06:22

XXXX@XXXXX.COM wrote:

OK, great, and I will keep in mind daily exercise and reducing my drug
dependencies. Because neither of us wants to do it. I don't want to drag
my ass out of bed every morning and run up and down the frickin
boardwalk and/or give up my drugs of choice and you and Costanza and
Seibel and 90 out of 96 yobbos are not as cool as you think you are when
it comes to being clued into hot technology and if you were honest would
say you are scared to death of learning anything new.

You drape yourselves in Lisp and prance about as if you are special when
you are not. Lisp is special. You are just average drones who have
wandered by mistake into this hallowed space and think it makes you
cool. It does not.

What would make you cool is an openness to good ideas. That One(tm)
mocked me for openness recently sealing his fate like Pete Rose.
Meanwhile, why is JMcC a genius aka clearly an alien? He sailed straight
past CPUs and instruction sets to this whacky idea that code should be
data. WTF? OK, he was helped by the RFE (reality) which requires that
code be data but he still gets more points than I ever will for noticing.

The good news is that you have your hands clapped over your ears and are
going Wooo! Wooo! Wooo! The bad news is it should have been your eyes.


No it doesn't unless you are subscribed to cells-devel and I did not
notice. Or clozure-cells or open-laszlo or adobe adam or father time or
I really do not care... game over? The yobbos sob openly and fall on
their swords: Kenny told them, and they mocked him, because he made fun
of McCLIM, one of their pet projects, violating the Code of the Pig.

Meanwhile the entire frickin world of programming moves on to automatic
state management, an order of magnitude more important than automatic
memory management.

As for Lispers...there is no such thing: using Lisp does not make you a
Lisper. Lisp is a state of mind. If you do not enjoy the state of
mind...talk about a self-finishing sentence!

k.x.o

 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Mirko.Vuko » Sat, 11 Oct 2008 20:01:10

n Oct 10, 1:06m, Kenny < XXXX@XXXXX.COM > wrote:
> >>> init>o>>>0
> >>> accessor>p>>>ing
> >>> initar> >>>dding
> >>> documentation "Padding is necessary for some >i>>>r order
> >>>inte>p>>>tions")
> >>>>>>(storage
> >>> gt;gt;>>itform nil
> >>> ac>e>>>r storage)))
> >>>(defmethod initialize-instance :after (>t>>> base) &key)
> >>> format t "Initializin> >>>e ~a~%" this)
> >>> setf (storage this) (make->r>>> (+ (dim this)
> >>> > >>> (padding this))
> >>> > gt;gt;>>init>a>>>lement 0d0>)>>>>
> >>>(defclass>s>>>a (base)
> >>> (du>m>>>> >>> initform 2d0
> >>> accessor dummy))
> >>> (:documenta>i>>>"This will implement a>hi>h>>>order interpolations
> >>>that needs pad>i>>>)
>
> >>>;; I would like to modify padding here.
> >>>(d>f>>>hod initialize-instance :after ((this sub1>)>>>ey)
> >>> format t "Init>al>z>>> sub ~a~%" this)
> >>> setf (pa>di>g>>>>s) 2))
>
> >>>To get what I want,>I >e>>>to do
>
> >>>>(make-instance >su>1>>>>adding 2)
>
> >>>but I>wo>l>>>refer to do just
>
> >>>>(make-ins>an>e>>sub1a)
>
> >>>This, of course does not work, ...
>
> >>I wish you h>d>>roved that with repl output...oh, christ, you are not
> >>talking about p>d>>ng, you are talking about the storage. Which takes me
> >>back to your>u>> of "This" which in turn takes me back to high school
> >>geometry and >r>>Wiseman who always said "T>er>'>>>o room for pronouns
> >>in geometry, >r>>>ilton.
>
> >>>because first the base's initialize-
>>>>>i>>tance method is called, which sort-of makes sense.
>
> >>That One>w>>l be along shortly to recommend either a metaclass or
> >>AspectL, I w>l>>recommend Cells which always does things In the Right
> >>Order(tm). And>h>>pily saves you from having to write i-i methods, that
> >>name>is>t>> long and I have carpal tunnel, dammit! I digress.
>
> >>A humbler "gee,>l>>'s do everything with CLOS!" solution would be to use
> >>an :a>o>>d method to set up the storage slot, doing so>/a>t>>/
> >>call-next-method to get the desired effect.
>
> >>But now we have >h>>CLOS tail wagging the dog, which I am afraid is you..
> >> hat ha>p>>s when things get more complicated and you run out of
> >>stu>id>G>>dispatch tricks? /Now/ we need That One. Or Cells.
>
> >>Interestin>l>> Cells was invented precisely to solve a problem with
> >>order of calc>l>>ion -- one where slots needed the value of slots from
> >>other instan>e>>as well as the same -- not to do reactive programming
> >>(the handies> >>me for this particular pet trick). Reactive just fell
> >>out of the>t>>e, which was the second sign to me that Cells was way
> >>cool. The firs> >>s how it trivially dispatched the daunting problem of
> >>ge>ti>g>complex gui geometry calculated in the right order.
>
> > Hmm. wi>l >ot change the code for now, but I'll keep it in mind.
>
> OK, great, an> I will keep in mind daily exercise and reduc
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Frank GOEN » Sat, 11 Oct 2008 20:11:02

enny < XXXX@XXXXX.COM > writes:


(in-package :cells)

(defmd base ()
(dim (c-in 12))
(padding (c-in 0)))

(defmd sub1a (base)
(dummy (c-in 2d0))
:padding (c-in 2))

(defun test-it ()
(describe (make-instance 'sub1a)))

CELLS> (test-it)
SUB1A0 is an instance of #<STANDARD-CLASS SUB1A>:
The following slots have :INSTANCE allocation:
.MD-STATE :AWAKE
.AWAKEN-ON-INIT-P NIL
.CELLS ((DUMMY . <0:A DUMMY/SUB1A0 = 2.0d0>)
(PADDING . <0:A PADDING/SUB1A0 = 2>)
(DIM . <0:A DIM/SUB1A0 = 12>))
.CELLS-FLUSHED NIL
ADOPT-CT 0
.MD-NAME SUB1A0
.FM-PARENT NIL
.VALUE NIL
ZDBG NIL
DIM 12
PADDING 2
DUMMY 2.0d0

;-)

Productivity? Yes, that's what I care about.

Cheers!
Frank

--
frgo(at)me(dot)com

"Don't ask me! I haven't been reading comp.lang.lisp long enough to
really know ..."
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Kenn » Sat, 11 Oct 2008 21:40:28

rank GOENNINGER wrote:
Don't forget the punch line:
(storage (c? (make-array (+ (^dim)(^padding))
:initial-element (^dummy))))
:dummy (c? (/ (^dim) 3d0)))))
STORAGE #(4.0d0 4.0d0 4.0d0 4.0d0 ...)

:)

kt
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by George Neu » Sun, 12 Oct 2008 05:19:17


I think it's time for Kenny's nap.

George
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Kenn » Sun, 12 Oct 2008 09:22:11


I think George does not know anything about Cells. Or Adam. Or Father
Time. Or OpenLaszlo. Hell, not even KR?

<sigh>

What can I say to help? Cells cells cells cells cells?

:)

hth, kenny
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by AndrThiem » Sun, 12 Oct 2008 13:21:02

Make a very cool looking website, full with Ajax and stuff.
Give a detailed manual and some how-to.
Publish some videos at youtube that show how cool Cells are.
So, basically do what they did for Rails (Ruby) with Cells for Lisp
and thousands of Lispers will use Cells next year.


Andr --
 
 
 

Can sub-classes initialize-instance modify base-classes slot *before* base's initialize-instance executes?

Post by Kenn » Sun, 12 Oct 2008 14:18:46


>> Make a very cool looking website, full with Ajax and stuff. >> Give a detailed manual and some how-to. >> Publish some videos at youtube that show how cool Cells are. >> So, basically do what they did for Rails (Ruby) with Cells for Lisp >> and thousands of Lispers will use Cells next year.

Sounds like work. And terribly un-Lispy: we welcome noobs with Emacs and
Slime and pretend it is an ISE.

Besides, if they were Lispers they would not need to be tricked into
discovering Cells, they just would. Enough have that I know enough not
to bother with those who have not. I'd like to see an AI tool parse
that sentence without killing the crew. I digress.

George Neuner recently posted brilliantly on the difference between
using Lisp and truly enjoying Lisp Mind (tm). Check it out.

best,kzo