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

Question: initialize-instance



On Tue May 18 08:34:45 1993, Cecil N. Huang writes:
  I have a problem which I only know how to solve by exiting and reentering
  MCL 2.0.  This has to do with initialize-instance:
 
 1.  First, I defined a class with some :initforms.
 2.  Then, I defined an initialize-instance on that class.
 *.   What I really wanted to do was define an initialize-instance :after
     so that the system's initialize-instance would still process the
     :initforms and the :initargs properly.

There are several methods of dealing with your problem.  The most
straightforward solution is to specialize the initialize instance,
performing actions after calling the next method.  The following
code fragment does exactly this for my-window:

(defclass my-window (window)
  nil)

(defmethod initialize-instance ((window my-window) &rest initargs)
  (declare (dynamic-extent initargs))
  (call-next-method)
  ;;; specialized code for my-window
  )

For an example see the scrolling-windows.lisp file in the examples
folder (in particular the initialize-instance method for scrolling-window).

If you want to define an after method, use a similar aproach,
by inserting no specialized code for my-window.

If you want to be able to undefine methods, the following code
(source unknown) will help.


mark


(defun undefine-method (generic-function)
  (setf generic-function (if (symbolp generic-function)
                           (fdefinition generic-function)
                           generic-function))
  (if (not (typep generic-function 'generic-function))
    (format t "~%~A is not a generic function." generic-function)
    (let ((method-to-be-removed
           (car (select-item-from-list
                 (generic-function-methods generic-function)
                 :window-title "Select a method to be removed"))))
      (when (y-or-n-dialog
             (format nil "OK to remove ~A?" method-to-be-removed))
        (remove-method generic-function method-to-be-removed)
        (format t "~%~A removed." method-to-be-removed)))))

(defun examine-methods (generic-function)
  (setf generic-function (if (symbolp generic-function)
                           (fdefinition generic-function)
                           generic-function))
  (if (not (typep generic-function 'generic-function))
    (format t "~%~A is not a generic function." generic-function)
    (let ((method-to-be-removed
           (car (select-item-from-list
                 (generic-function-methods generic-function)
                 :window-title "Select a method to be removed")))))))

(defun retrieve-methods (generic-function &optional class)
  (setf generic-function (if (symbolp generic-function)
                           (fdefinition generic-function)
                           generic-function))
  (if (not (typep generic-function 'generic-function))
    (format t "~%~A is not a generic function." generic-function)
    (mapcar #'(lambda (u) (cons (slot-value (first (slot-value u
'ccl::specializers))
                                            'ccl::name) u))
            (generic-function-methods generic-function))))

#|

(undefine-method #'initialize-instance)

;; then select the particular method to "undefine"

|#