From: Karol Skocik
Subject: initialize-instance question
Date: 
Message-ID: <1162402758.097257.236460@e64g2000cwd.googlegroups.com>
Hi,
  I have a problem with constructing my instances - to be more concrete
:

(defclass entity ()
  ((id :type u8
       :initarg :id
       :reader id-of)
   (ent-type :type u8
	     :initarg :ent-type
	     :reader ent-type-of)
   (x :type u8
      :initarg :x
      :accessor x-of)
   (y :type u8
      :initarg :y
      :accessor y-of)
   ))

(defmethod initialize-instance :after ((entity movable-entity) &rest
initargs)
  (declare (ignore initargs))
  (format t "////////// x = ~a~%" (x-of entity)))

Now, when I do make-instance, the slot is unbound which is ok according
to hyperspec - it used default initforms which is nil. But then, what's
the point of initialize-instance?

when this:

(let ((e (make-instance 'movable-entity
				:id 0
				:ent-type 1
				:x 10
				:y 20
				:move-time 300)))
	  e)

does not propagate the values I supply to initialize-instance and
ignores them?

Is there any way to make it behave I would expect?

Thank you,
  Karol

From: Ken Tilton
Subject: Re: initialize-instance question
Date: 
Message-ID: <JF52h.15$VP3.11@newsfe09.lga>
Karol Skocik wrote:
> Hi,
>   I have a problem with constructing my instances - to be more concrete
> :
> 
> (defclass entity ()
>   ((id :type u8
>        :initarg :id
>        :reader id-of)
>    (ent-type :type u8
> 	     :initarg :ent-type
> 	     :reader ent-type-of)
>    (x :type u8
>       :initarg :x
>       :accessor x-of)
>    (y :type u8
>       :initarg :y
>       :accessor y-of)
>    ))
> 
> (defmethod initialize-instance :after ((entity movable-entity) &rest
> initargs)
>   (declare (ignore initargs))
>   (format t "////////// x = ~a~%" (x-of entity)))
> 
> Now, when I do make-instance, the slot is unbound which is ok according
> to hyperspec - it used default initforms which is nil. But then, what's
> the point of initialize-instance?

You are probably as confused as is your question. Where the hell is the 
definition of movable-entity? Don't answer that, I know you were just 
typing into an article vs. cutting/pasting actual code, so...

> 
> when this:
> 
> (let ((e (make-instance 'movable-entity
> 				:id 0
> 				:ent-type 1
> 				:x 10
> 				:y 20
> 				:move-time 300)))
> 	  e)
> 
> does not propagate the values I supply to initialize-instance and
> ignores them?

Yeah, it does. What you typed above is not what you actually tested, or 
you are asking three questions at once, or something else.

kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Karol Skocik
Subject: Re: initialize-instance question
Date: 
Message-ID: <1162407715.921982.241250@f16g2000cwb.googlegroups.com>
Sorry,
  I forgot to post movable-entity. Here it is:

(defclass movable-entity (entity)
  ((x-real :type float
	   :initarg :x-real
	   :accessor x-real-of)
   (y-real :type float
	   :initarg :y-real
	   :accessor y-real-of)
   (x-destination :type float
		  :initform 0
		  :accessor x-destination-of)
   (y-destination :type float
		  :initform 0
		  :accessor y-destination-of)
   (x-delta :type float
	    :initform 0
	    :accessor x-delta-of)
   (y-delta :type float
	    :initform 0
	    :accessor y-delta-of)
   (move-timestamp :type fixed ;; time of last move update
		   :initform 0
		   :accessor move-timestamp-of)
   (stop-time :type fixed
	      :initform 0
	      :accessor stop-time-of)
   (move-time :type u32	;; time required to move between 2 cells
 	      :initarg :move-time
	      :accessor move-time-of)
   (is-moving :type boolean
	      :initform nil
	      :accessor is-moving-of)
   ))

Since, initialize-instance did not work for me I made something like
this :

(defun make-movable-entity (id type x y speed)
  (make-instance 'movable-entity
		 :id id
		 :ent-type type
		 :x x
		 :y y
		 :move-time speed
		 :x-real (+ x 0.5)
		 :y-real (+ y 0.5)))

which I would like to replace with initialize-instance.

I had maybe some stale file, with wrong definition or so... will try
again.

Thanks for making me sure it initialize-instance get's arguments from
make-instance and not one taken from initforms...

Karol

Ken Tilton wrote:
> Karol Skocik wrote:
> > Hi,
> >   I have a problem with constructing my instances - to be more concrete
> > :
> >
> > (defclass entity ()
> >   ((id :type u8
> >        :initarg :id
> >        :reader id-of)
> >    (ent-type :type u8
> > 	     :initarg :ent-type
> > 	     :reader ent-type-of)
> >    (x :type u8
> >       :initarg :x
> >       :accessor x-of)
> >    (y :type u8
> >       :initarg :y
> >       :accessor y-of)
> >    ))
> >
> > (defmethod initialize-instance :after ((entity movable-entity) &rest
> > initargs)
> >   (declare (ignore initargs))
> >   (format t "////////// x = ~a~%" (x-of entity)))
> >
> > Now, when I do make-instance, the slot is unbound which is ok according
> > to hyperspec - it used default initforms which is nil. But then, what's
> > the point of initialize-instance?
>
> You are probably as confused as is your question. Where the hell is the
> definition of movable-entity? Don't answer that, I know you were just
> typing into an article vs. cutting/pasting actual code, so...
>
> >
> > when this:
> >
> > (let ((e (make-instance 'movable-entity
> > 				:id 0
> > 				:ent-type 1
> > 				:x 10
> > 				:y 20
> > 				:move-time 300)))
> > 	  e)
> >
> > does not propagate the values I supply to initialize-instance and
> > ignores them?
>
> Yeah, it does. What you typed above is not what you actually tested, or
> you are asking three questions at once, or something else.
>
> kt
>
> --
> Cells: http://common-lisp.net/project/cells/
>
> "I'll say I'm losing my grip, and it feels terrific."
>     -- Smiling husband to scowling wife, New Yorker cartoon
From: Ken Tilton
Subject: Re: initialize-instance question
Date: 
Message-ID: <tB62h.23$1h5.5@newsfe08.lga>
Karol Skocik wrote:
> Sorry,
>   I forgot to post movable-entity. Here it is:
> 
> (defclass movable-entity (entity)
>   ((x-real :type float
> 	   :initarg :x-real
> 	   :accessor x-real-of)
>    (y-real :type float
> 	   :initarg :y-real
> 	   :accessor y-real-of)
>    (x-destination :type float
> 		  :initform 0
> 		  :accessor x-destination-of)
>    (y-destination :type float
> 		  :initform 0
> 		  :accessor y-destination-of)
>    (x-delta :type float
> 	    :initform 0
> 	    :accessor x-delta-of)
>    (y-delta :type float
> 	    :initform 0
> 	    :accessor y-delta-of)
>    (move-timestamp :type fixed ;; time of last move update
> 		   :initform 0
> 		   :accessor move-timestamp-of)
>    (stop-time :type fixed
> 	      :initform 0
> 	      :accessor stop-time-of)
>    (move-time :type u32	;; time required to move between 2 cells
>  	      :initarg :move-time
> 	      :accessor move-time-of)
>    (is-moving :type boolean
> 	      :initform nil
> 	      :accessor is-moving-of)
>    ))
> 
> Since, initialize-instance did not work for me I made something like
> this :
> 
> (defun make-movable-entity (id type x y speed)
>   (make-instance 'movable-entity
> 		 :id id
> 		 :ent-type type
> 		 :x x
> 		 :y y
> 		 :move-time speed
> 		 :x-real (+ x 0.5)
> 		 :y-real (+ y 0.5)))

No.

> 
> which I would like to replace with initialize-instance.

Yes.

> 
> I had maybe some stale file, with wrong definition or so...

Yes. Perhaps you forgot originally to inherit from entity, or perhaps 
you initially did not have an initarg for X in entity, or...

> will try
> again.

You might try (describe entity) as the first line of your i-i method, 
and printing the list of initargs. If you still have trouble, I mean.

kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Thomas A. Russ
Subject: Re: initialize-instance question
Date: 
Message-ID: <ymi64dybrro.fsf@sevak.isi.edu>
"Karol Skocik" <············@gmail.com> writes:

> I had maybe some stale file, with wrong definition or so... will try
> again.
> 
> Thanks for making me sure it initialize-instance get's arguments from
> make-instance and not one taken from initforms...

I wonder if one potential problem may have been a primary method on
INITIALIZE-INSTANCE that didn't have a (CALL-NEXT-METHOD) in it, which
was preventing the normal slot initialization?

For example:

USER> (defclass foo () ((bar :initarg :bar)))
#<STANDARD-CLASS FOO>

;; This will defeat normal slot initialization:
USER> (defmethod initialize-instance ((i foo) &rest args))
#<STANDARD-METHOD INITIALIZE-INSTANCE (FOO)>

USER> (make-instance 'foo :bar 8)
#<FOO @ #x44e617a>

USER> (describe *)
#<FOO @ #x44e617a> is an instance of #<STANDARD-CLASS FOO>:
 The following slots have :INSTANCE allocation:
  BAR   <unbound>


;; Whereas if you leave things alone...

USER> (defclass bar () ((foo :initarg :foo)))
#<STANDARD-CLASS BAR>

USER> (make-instance 'bar :foo 8)
#<BAR @ #x44e6992>

USER> (describe *)
#<BAR @ #x44e6992> is an instance of #<STANDARD-CLASS BAR>:
 The following slots have :INSTANCE allocation:
  FOO   8

;; Or at least call the next method:
USER> (defmethod initialize-instance ((i bar) &rest args)
           (call-next-method))
#<STANDARD-METHOD INITIALIZE-INSTANCE (BAR)>

USER> (make-instance 'bar :foo 10)
#<BAR @ #x44e714a>

;; It works!
USER> (describe *)
#<BAR @ #x44e714a> is an instance of #<STANDARD-CLASS BAR>:
 The following slots have :INSTANCE allocation:
  FOO   10
USER> 



-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Ken Tilton
Subject: Re: initialize-instance question
Date: 
Message-ID: <6ba2h.103$Rj2.87@newsfe10.lga>
Karol Skocik wrote:

> I had maybe some stale file, with wrong definition or so... will try
> again.

btw, this /is/ part of the learning curve with dynamic languages such as 
Lisp and Prolog still based on source files unlike Smalltalk: you can 
change the source but not change what runs. Methods, for example, do not 
go away just because you delete them or change their specialization.

The situation is worse if you have not automated your builds with some 
form of defsystem. There is a crappy hack called ASDF most people use, 
or look for mk::defsystem.

kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Alan Crowe
Subject: Re: initialize-instance question
Date: 
Message-ID: <86k62fghds.fsf@cawtech.freeserve.co.uk>
"Karol Skocik" <············@gmail.com> writes:

> Hi,
>   I have a problem with constructing my instances - to be more concrete
> :
> 
> (defclass entity ()
>   ((id :type u8
>        :initarg :id
>        :reader id-of)
>    (ent-type :type u8
> 	     :initarg :ent-type
> 	     :reader ent-type-of)
>    (x :type u8
>       :initarg :x
>       :accessor x-of)
>    (y :type u8
>       :initarg :y
>       :accessor y-of)
>    ))
> 
> (defmethod initialize-instance :after ((entity movable-entity) &rest
> initargs)
>   (declare (ignore initargs))
>   (format t "////////// x = ~a~%" (x-of entity)))
> 
> Now, when I do make-instance, the slot is unbound which is ok according
> to hyperspec - it used default initforms which is nil. But then, what's
> the point of initialize-instance?
> 
> when this:
> 
> (let ((e (make-instance 'movable-entity
> 				:id 0
> 				:ent-type 1
> 				:x 10
> 				:y 20
> 				:move-time 300)))
> 	  e)
> 
> does not propagate the values I supply to initialize-instance and
> ignores them?
> 
> Is there any way to make it behave I would expect?

I'm not seeing a problem with simplified test code:

CL-USER> (defclass foo ()
           ((bar :accessor bar 
                 :initarg :bar)))
#<STANDARD-CLASS FOO {480E0BBD}>

CL-USER> (defmethod initialize-instance :after ((foo foo) &key afterthought)
           (declare (ignore afterthought))
           (format t "~&postinitialize snoop: ~A" (bar foo)))

#<STANDARD-METHOD INITIALIZE-INSTANCE :AFTER (FOO) {4831E71D}>

CL-USER> (make-instance 'foo :bar "thing")
postinitialize snoop: thing
#<FOO {48321945}>

Your example is incomplete. Are you sure that movable-entity
is inheriting correctly from entity?

(defclass movable-entity (entity) ... etc

Alan Crowe
Edinburgh
Scotland