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

Re: printers for structures



At 17:15 3/25/93 +0100, Abdel Kader Diagne wrote:
>Hi,
>
>I'm looking for a way of using optional printers for structures. In some
>cases I like to use the default lisp printer, and in other cases a user-defined
>pretty printer; e.g., what I want is to switch (depending on the value of a
>pre-defined variable) between the default printer and a user-defined
>pretty-printer for a given structure.
>Any idea how to do that?
>
>Thanks in advance.
>---Kader.

The following works in Macintosh Common Lisp. I haven't tested it
anywhere else.

------------------------------------------------------------------------

(defstruct foo x y)

(defvar *print-foos-pretty* nil)

(defmethod print-object ((o foo) stream)
  (if *print-foos-pretty*
    (format stream "#<A ~s structure with slots ~s: ~s and ~s: ~s>"
            'foo 'x (foo-x o) 'y (foo-y o))
    (call-next-method)))

#|
; Test code
(let ((foo (make-foo))
      (*print-foos-pretty* nil))
  (print foo)
  (setq *print-foos-pretty* t)
  (print foo)
  nil)
|#