home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / lisp / 2390 < prev    next >
Encoding:
Text File  |  1992-09-09  |  2.5 KB  |  86 lines

  1. Newsgroups: comp.lang.lisp
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!sdd.hp.com!ux1.cso.uiuc.edu!m.cs.uiuc.edu!m.cs.uiuc.edu!liberte
  3. From: liberte@cs.uiuc.edu (Daniel LaLiberte)
  4. Subject: Re: Function to print in #S format
  5. In-Reply-To: charest@Aig.Jpl.Nasa.Gov's message of Wed, 9 Sep 1992 01:10:22 GMT
  6. Message-ID: <LIBERTE.92Sep9132646@birch.cs.uiuc.edu>
  7. Followup-To: comp.lang.lisp
  8. Sender: news@m.cs.uiuc.edu (News Database (admin-Mike Schwager))
  9. Organization: University of Illinois, Urbana-Champaign, Dept CS
  10. References: <SJAMESON.92Sep8161926@fergie.dnet.ge.com>
  11.     <1992Sep9.011022.14516@jpl-devvax.jpl.nasa.gov>
  12. Date: Wed, 9 Sep 1992 19:26:46 GMT
  13. Lines: 71
  14.  
  15. In article <1992Sep9.011022.14516@jpl-devvax.jpl.nasa.gov> charest@Aig.Jpl.Nasa.Gov (Len Charest) writes:
  16.  
  17.    3. (mutually exclusive) Use CLOS. Ha Ha only serious.
  18.  
  19.  
  20. I took this suggestion seriously.  It wasnt obvious (to me) at first
  21. how one is supposed to print an object readably.  I wrote the
  22. following to help.  Please comment.
  23.  
  24. Dan LaLiberte
  25. liberte@cs.uiuc.edu
  26. (Join the League for Programming Freedom: league@prep.ai.mit.edu)
  27. --------
  28. ;;; Mixin to facilitate printing objects readably.
  29. ;;; Author: Daniel LaLiberte (liberte@ncsa.uiuc.edu) September 1992
  30.  
  31. #|
  32. Any class of instances may be printed with non-nil
  33. *print-readably* by mixing in the print-mixin class.
  34. A print-slots method must be defined for each class
  35. to print all of the slots needed to reconstruct the instance.  
  36. Any such slots must, of course, also have an :initarg.
  37. |#
  38.  
  39. (defclass print-mixin ()
  40.   ())
  41.  
  42. (defmethod print-object :around ((object print-mixin) stream)
  43.   "Print the object readably if *print-readably* is non-nil."
  44.   (if *print-readably*
  45.     (progn
  46.       (format stream "~%#.(make-instance '~s " (class-name (class-of object)))
  47.       (print-slots object stream)
  48.       (format stream ")"))
  49.     (call-next-method object stream))
  50.   object)
  51.  
  52. (defgeneric print-slots (print-mixin t)
  53.   (:method-combination progn))
  54.  
  55. (defmethod print-slots progn ((object print-mixin) stream)
  56.   (declare (ignore stream)))
  57.  
  58. #|
  59.  
  60. (defclass foo1 (print-mixin)
  61.   ((foo1 :accessor foo1 :initarg :foo1 :initform 1) ))
  62.  
  63. (defmethod print-slots progn ((object foo1) stream)
  64.   (format stream "~%   :foo1 ~s " (foo1 object)))
  65.  
  66.  
  67. (defclass foo2 (print-mixin)
  68.   ((foo2 :accessor foo2 :initarg :foo2 :initform 2) ))
  69.  
  70. (defmethod print-slots progn ((object foo2) stream)
  71.   (format stream "~%   :foo2 ~s " (foo2 object)))
  72.  
  73.  
  74. (defclass foo (foo1 foo2)
  75.   ())
  76.  
  77. (let ((*print-readably* t))
  78.   (format nil "~%~s" (make-instance 'foo :foo2 4)))
  79.  
  80. => "
  81.  
  82. #.(make-instance 'FOO 
  83.    :foo1 1 
  84.    :foo2 4 )"
  85. |#
  86.