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


Here's a fix to the DESCRIBE generic function to handle unbound slots.  The
code as supplied with AAAI PCL breaks when an unbound slot is encountered:

(defmethod describe ((obj object) &optional (stream t))
  (let* ((class (class-of obj))
	 (slotds (slots-to-inspect class obj))
	 (max-slot-name-length 0)
	 (instance-slotds ())
	 (class-slotds ())
	 (other-slotds ()))
    (flet ((adjust-slot-name-length (name)
	     (setq max-slot-name-length
		   (max max-slot-name-length
			;; format nil is necessary so we count the length of
			;; any package prefix if its going to get printed
			(length (format nil "~S" name)))))
	   (describe-slot (name &optional (allocation () alloc-p))
	     (if (slot-boundp obj name)
	       (format stream
		       "~% ~:[~*~;~A~]~V@S  ~S"
		       alloc-p allocation
		       name (slot-value obj name))
	       (format stream
		       "~% ~:[~*~;~A~]~V@S"
		       alloc-p allocation
      ;; Figure out a good width for the slot-name column.
      (dolist (slotd slotds)
	(adjust-slot-name-length (slotd-name slotd))
	(case (slotd-allocation slotd)
	  (:instance (push slotd instance-slotds))
	  (:class  (push slotd class-slotds))
	  (otherwise (push slotd other-slotds))))
      (setq max-slot-name-length  (min (+ max-slot-name-length 3) 30))
      (format stream "~%~S is an instance of class ~S:" obj class)

      (when instance-slotds
	(format stream "~% The following slots have :INSTANCE allocation:")
	(dolist (slotd (nreverse instance-slotds))
	  (describe-slot (slotd-name slotd))))

      (when class-slotds
	(format stream "~% The following slots have :CLASS allocation:")
	(dolist (slotd (nreverse class-slotds))
	  (describe-slot (slotd-name slotd))))

      (when other-slotds 
	(format stream "~% The following slots have allocation as shown:")
	(dolist (slotd (nreverse other-slotds))
	  (describe-slot (slotd-name slotd)
			 (slotd-allocation slotd))))

Here's another hack which allows you to see more information when
describing classes.  It just allows the default object method to be called
instead of DESCRIBE-CLASS. 

(defmethod describe ((obj standard-class) &optional (stream t))
I'd use UNDEFMETHOD to do this if I knew how it worked. 

If some of the slots of standard-class should be hidden from the DESCRIBE
mechanism, I suggest specializing the SLOTS-TO-INSPECT method for
standard-class.  Otherwise, its really useful to see all the information
that's there.