Describing objects

Everyone writes new methods for print-object for specialized printing of their own objects. But the output of describe can be specialized as well via describe-object. For example, if you have an object that has a vector of keys and a corresponding vector of values, the standard describe output might not show them in a way that’s very readable:

* (describe record)
#<RECORD {1002A87A03}>
  [standard-object]

Slots with :INSTANCE allocation:
  SOURCE  = #P"data.txt"
  ID      = 47
  KEYS    = #("Rating" "Unit Price" "Description" "Acquired Date")
  VALS    = #(7.2 21 "Blue-fringed tarpaulin with star pattern"..

You can add your own primary or auxiliary methods to change what describe prints:

(defmethod describe-object :after ((record record) stream)
  (write-line "Data values:")
  (loop for key across (keys record)
        for val across (vals record)
        do (format stream "  ~A: ~A~%" key val)))

Then the describe output can be more readable:

#<RECORD {1002B54023}>
  [standard-object]

Slots with :INSTANCE allocation:
  SOURCE  = #P"data.txt"
  ID      = 47
  KEYS    = #("Rating" "Unit Price" "Description" "Acquired Date")
  VALS    = #(7.2 21 "Blue-fringed tarpaulin with star pattern"..
Data values:
  Rating: 7.2
  Unit Price: 21
  Description: Blue-fringed tarpaulin with star pattern
  Acquired Date: 3532942160