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

Re: uses of call-next-method



    From: Richard Fritzson <fritzson@bigburd.PRC.Unisys.Com>

    I hate to post what seems to be a simple question, but I must be
    missing something. I would appreciate anyone's comments on this.

    I want to define a method which uses a preexisting less specific
    method using call-next-method. A simple example is:

    (defclass position () ((x :initform 0 :type number) 
    				  (y :initform 0 :type number)))

    (defmethod move ((point position) dx dy)
    	(with-slots (point)
    		(setf x (+ x dx) y (+ y dy)))
    	point)

    (defclass 3d-position (position) ((z :initform 0 
    							 :type number)))

    ; and now, define a version of 3d-move which uses the 2d move

    (defmethod move ((point 3d-position) dx dy dz)
    	(with-slots (point) 
    		(setf z (+ z dz))
    		(call-next-method)))

    This doesn't work because it passes three arguments to the
    (move (position)) method when only two are expected.

Actually, your real problem is that PCL is not signalling the correct
error.

According to CLOS, your two methods definitions for MOVE are
incompatible.  Evaluating the second method definition should signal an
error.  This is because the two methods do not have congruent argument
lists.  An alternate way to write this code would be.

    (defmethod move ((point position) dx dy dz)
      (declare (ignore dz))
    	(with-slots (point)
        (setf x (+ x dx)
              y (+ y dy)))
    	point)

    (defmethod move ((point 3d-position) dx dy dz)
    	(with-slots (point) 
        (setf z (+ z dz))
        (call-next-method)))