objectcommon-lispelispcl-lib

How to implement `cl-print-object`?


I am using 'eieio and 'cl-lib and I'm trying to define the object-print method for my class as described here:

https://www.gnu.org/software/emacs/manual/html_mono/eieio.html#Basic-Methods

(cl-defmethod object-print ((this data-object) &optional strings)
  "Return a string with a summary of the data object as part of the name."
  (apply #'cl-call-next-method this
         (format " value: %s" (render this))
         strings))

I want to do this so I can see the contents of my object instances when I'm using edebug.

When I implement this example in my own code it produces the error message the function ‘render’ is not known to be defined.

In addition to the error message, Flycheck suggests: ‘object-print’ is an obsolete generic function (as of 26.1); use ‘cl-print-object’ instead. I cannot find any documentation on the cl-print-object function when I Google, and I am failing at guessing how to implement it:

(cl-defmethod cl-print-object ((this card) &optional strings)
  "Return a string with a summary of the data object as part of the name."
  (apply #'cl-call-next-method this
         (format " value: %s" (slot-value this 'value))
         strings))

I do not know what to provide instead of (slot-value this 'value). It does not seem to want a slot.

How can I implement the cl-print-object function? Or is there a better way to see the contents of an EIEIO object instance when debugging inside edebug?


Solution

  • I don't understand elisp's CLOSoid system, but something like this is at least a start:

    (defclass foo ()
      ((x :initform 1
          :inirarg :x
          :reader foo-x)))
    
    (defmethod cl-print-object ((f foo) stream)
      (princ (format "#<%s x %s>"
                     (class-name (class-of f))
                     (foo-x f))
             stream))
    

    Now:

    ELISP> (make-instance 'foo :x 2)
    #<foo x 2>
    ELISP> (defclass bar (foo) ())
    bar
    ELISP> (make-instance 'bar :x 2)
    #<bar x 2>
    

    Obviously in real life you want to extract more information from the object: I don't know if there is an equivalent of CL's print-unreadable-object which provides a nice approach to this without requiring you do to all the work by hand.