home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / lisp / 2388 < prev    next >
Encoding:
Internet Message Format  |  1992-09-08  |  2.7 KB

  1. Path: sparky!uunet!spool.mu.edu!yale.edu!qt.cs.utexas.edu!cs.utexas.edu!gateway
  2. From: gadbois@mcc.com (David Gadbois)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: Function to print in #S format
  5. Date: 8 Sep 1992 19:40:34 -0500
  6. Organization: UTexas Mail-to-News Gateway
  7. Lines: 47
  8. Sender: daemon@cs.utexas.edu
  9. Message-ID: <19920909004003.1.GADBOIS@CLIO.MCC.COM>
  10. References: <SJAMESON.92Sep8161926@fergie.dnet.ge.com>
  11. NNTP-Posting-Host: cs.utexas.edu
  12.  
  13.     Date: Tue, 8 Sep 1992 15:19 CDT
  14.     From: sjameson@fergie.dnet.ge.com (Stephen M Jameson)
  15.  
  16.     This ought to be a FAQ, but I haven't seen it.  How do I print a
  17.     structure in #S-format, i.e. so that the reader will automatically
  18.     reconstruct the structure?  The application is obvious: I would like
  19.     to have a :PRINT-FUNCTION specified in the DEFSTRUCT which will
  20.     print in a concise, non-readable format if a certain switch is set,
  21.     and print in the verbose, readable format if the switch is not set.
  22.     On the Symbolics I figured out a way to do it but it did not seem
  23.     portable, and CLTL2 doesn't mention anything beyond the semantics of
  24.     #S format, and that if you don't specify a :PRINT-FUNCTION, "a
  25.     function is provided for the structure which will print out all its
  26.     slots using #S syntax."
  27.  
  28. If you have CLOS hooked in, you can define a method on PRINT-OBJECT that
  29. checks your switch (which probably should be just *PRINT-READABLY*) and
  30. calls CALL-NEXT-METHOD if it is not set.  I do this so often I have
  31. written a macro to deal with it:
  32.  
  33. (defmacro define-print-method ((object-var object-type stream-var) &body body)
  34.   `(defmethod print-object ((,object-var ,object-type) ,stream-var)
  35.      (if *print-readably*
  36.      #+Genera
  37.      ;; The default structure printer checks this and calls
  38.      ;; PRINT-UNREADABLE-OBJECT if it is not set.
  39.      (let ((scl:*print-structure-contents* t))
  40.        (call-next-method))
  41.      #-Genera
  42.      (call-next-method)
  43.      (print-unreadable-object (,object-var ,stream-var :type t)
  44.        ,@body))))
  45.  
  46. Note that CMU CL v15 did not let you use structure types as
  47. specializers, so this will not work there.  I have not checked v16 yet
  48. to see if they have fixed it.  For Lucid 4.0.x, you have to define
  49. *PRINT-READABLY* and PRINT-UNREADABLE-OBJECT yourself.  At least Allegro
  50. and MCL get it right without extra kludgery.  I sure will be happy when
  51. the draft proposed CL standard gets accepted so I'll never have to worry
  52. about portability problems again :-).
  53.  
  54. If you do not have CLOS, you can always how the structure's print
  55. function do the #S printing by hand, but that is pretty gross.  Or you
  56. could use implementation dependent accessors to get at the slot names
  57. and values (this is in the FAQ) and write a general printer.
  58.  
  59. --David Gadbois
  60.