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

Re: self and CLOS

>I have a question that ya'll may be able to help me with.  I'm learning the
>CLOS system (I've been programing in Object Pascal & C++ for some time), but a
>few of it's fine points are seeming to escape me.  The first is how you refer 
>to your "self".  As an example here is a very simple class that represents a
>graphic object which can be dragged, resized, and knows how to draw itself.
>(defclass square-thing ()
>    ((fTop   :accessor top
>             :init-form 10
>             :init-arg :top)
>     (fLeft  :accessor left
>             :init-form 10
>             :init-arg :left)
>     (fBottom :accessor bottom
>             :init-form 100
>             :init-arg :bottom)
>     (fRight :accessor right
>             :init-form 100
>             :init-arg :right))
>OK, so far so good.  Now I want to define a method which will be used for this 
>thing to draw itself on the screen each time.  I wanted to define the method
>so that the coordinates are optional, otherwise it just draws itself in it's 
>current place (as defined by the slots).  My problem is, how do I specify the
>default values of the &optional arguments without refering to a "self".
>The following example is what I am trying (but is wrong):
>(defmethod draw ((me square-thing) (wind view)
>                  &optional (t (me top)) (l (me left))
>                            (b (me bottom)) (r (me right)))
> (paint-rect wind l t r b)
> (frame-rect wind l t r b))
> I don't want to have to kludge the lambda list with 0 (or neg) defaults (and
>then test if they are zero or negative). There must be a way to do this correctly.
>               Jeffrey

You simply have to use the accessors you defined above...
i.e. the top left bottom and right accessor methods:

(defmethod draw ((me square-thing) (wind view)
                  &optional (t (top me)) (l (left me))
                            (b (bottom me)) (r (right me)))
 (paint-rect wind l t r b)
 (frame-rect wind l t r b))

* Guillaume Cartier                 (514) 844-5294 (maison)         *
* L.A.C.I.M.                        (514) 987-4290 (bureau)         *
* Universite du Quebec a Montreal   (514) 987-8477 (telecopieur)    *
* Montreal, Quebec, Canada          cartier@math.uqam.ca (internet) *