[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Initialize-instance



>Date: Mon, 6 Jul 92 10:38:38 -0400
>From: jbk@world.std.com (Jeffrey B Kane)
>To: info-mcl@cambridge.apple.com
>Subject: Initialize-instance
>
>I have another couple of quick CLOS related questions.  The first has to do
>with defining my own "initialize-instance" method for my classes. For my
>simplest example, I didn't need any parameters, so I tried:
>
>(defclass twindow (window)
>  ((garbage :initarg :garbage     ; holds some value that I might need
>            :initform nil
>            :accessor garbage)))
>
>(defmethod initialize-instance ((me twindow) &rest)
>  (print "Hi, I'm initializing an instance of a twindow")
>  (initialize-instance (coerce me window)))
>
Forget the coerce command, that's not what you want at all.
Here's two different ways to do this:

; This doesn't shadow the previous definition, it just runs BEFORE it.
; Also check out AFTER methods.
(defmethod initialize-instance :before ((me twindow) &rest rest)
  (declare (ignore rest))
  (print "Hi, I'm initializing an instance of a twindow"))

; This wraps AROUND the prevous definition. You can provide 
; different args to CALL-NEXT-METHOD.
(defmethod initialize-instance :around ((me twindow) &rest rest)
  (declare (ignore rest))
  (print "Hi, I'm about to initialize an instance of a twindow")
  (call-next-method)    
  (print "Hi, I just initialized an instance of a twindow"))
   

>As a second question, what is the prefered way of calling an inherited method.
>In object pascal I just call
>
>inherited old_method(var1 var2 var3);
>
>in C I need to explicitly call it using the class name:
>
>parentClass::old_method(var1 var2 var3);
>
>Am I doing it correctly in the above example, i.e.
>
>(initialize-instance (coerce me window)))
>
>[I'm trying to call the inherited initialize-instance]

As indicated above, you don't need to explicitly call it when you 
use :before or :after methods. Within an :around method, you can
use CALL-NEXT-METHOD.



>I'm still looking for the CLOS tutorial book.  I got the book 
>"Understanding CLOS" but it seems that it is mostly a rehash of
>the specification with little insight into it's actual use.
>
>Too bad I can't park at Quantum Books :)
>
There's a garage under Quantum; I think you might be able to
get validate parking (this is in Cambridge, Mass., for you readers
at home..)