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

Re: printers for structures

Bill St. Clair (bill@cambridge.apple.com) writes:

  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))

This works in Allegro 4.1 also.  BUT, in the ANSI draft, structures
are not required to print via PRINT-OBJECT, although that might seem
counter-intuitive.  The draft says (p 8-10)

  If :PRINT-FUNCTION [is] not supplied ... then a default printing
  function is provided for the structure that prints out all its slots
  using #S syntax. 

  Supplying :PRINT-FUNCTION ... is equivalent to defining an
  appropriate method on the PRINT-OBJECT generic function.

The first paragraph would seem to indicate that every structure has a
:PRINT-FUNCTION function, whether you define it or not, and thus
:PRINT-OBJECT methods on an included structure class aren't inherited.
In the second paragraph, it's not clear what "equivalent" means, as
Sandra Loosemoore has pointed out in public review comments.  For one
thing, :PRINT-FUNCTION functions have different argument structure

This confusion will probably be rectified in the next draft, but for
now it's yet another example of how hard it is to properly integrate
new features (CLOS) and old (DEFSTRUCT).

John Burger