home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.lisp
- Path: sparky!uunet!elroy.jpl.nasa.gov!sdd.hp.com!ux1.cso.uiuc.edu!m.cs.uiuc.edu!m.cs.uiuc.edu!liberte
- From: liberte@cs.uiuc.edu (Daniel LaLiberte)
- Subject: Re: Function to print in #S format
- In-Reply-To: charest@Aig.Jpl.Nasa.Gov's message of Wed, 9 Sep 1992 01:10:22 GMT
- Message-ID: <LIBERTE.92Sep9132646@birch.cs.uiuc.edu>
- Followup-To: comp.lang.lisp
- Sender: news@m.cs.uiuc.edu (News Database (admin-Mike Schwager))
- Organization: University of Illinois, Urbana-Champaign, Dept CS
- References: <SJAMESON.92Sep8161926@fergie.dnet.ge.com>
- <1992Sep9.011022.14516@jpl-devvax.jpl.nasa.gov>
- Date: Wed, 9 Sep 1992 19:26:46 GMT
- Lines: 71
-
- In article <1992Sep9.011022.14516@jpl-devvax.jpl.nasa.gov> charest@Aig.Jpl.Nasa.Gov (Len Charest) writes:
-
- 3. (mutually exclusive) Use CLOS. Ha Ha only serious.
-
-
- I took this suggestion seriously. It wasnt obvious (to me) at first
- how one is supposed to print an object readably. I wrote the
- following to help. Please comment.
-
- Dan LaLiberte
- liberte@cs.uiuc.edu
- (Join the League for Programming Freedom: league@prep.ai.mit.edu)
- --------
- ;;; Mixin to facilitate printing objects readably.
- ;;; Author: Daniel LaLiberte (liberte@ncsa.uiuc.edu) September 1992
-
- #|
- Any class of instances may be printed with non-nil
- *print-readably* by mixing in the print-mixin class.
- A print-slots method must be defined for each class
- to print all of the slots needed to reconstruct the instance.
- Any such slots must, of course, also have an :initarg.
- |#
-
- (defclass print-mixin ()
- ())
-
- (defmethod print-object :around ((object print-mixin) stream)
- "Print the object readably if *print-readably* is non-nil."
- (if *print-readably*
- (progn
- (format stream "~%#.(make-instance '~s " (class-name (class-of object)))
- (print-slots object stream)
- (format stream ")"))
- (call-next-method object stream))
- object)
-
- (defgeneric print-slots (print-mixin t)
- (:method-combination progn))
-
- (defmethod print-slots progn ((object print-mixin) stream)
- (declare (ignore stream)))
-
- #|
-
- (defclass foo1 (print-mixin)
- ((foo1 :accessor foo1 :initarg :foo1 :initform 1) ))
-
- (defmethod print-slots progn ((object foo1) stream)
- (format stream "~% :foo1 ~s " (foo1 object)))
-
-
- (defclass foo2 (print-mixin)
- ((foo2 :accessor foo2 :initarg :foo2 :initform 2) ))
-
- (defmethod print-slots progn ((object foo2) stream)
- (format stream "~% :foo2 ~s " (foo2 object)))
-
-
- (defclass foo (foo1 foo2)
- ())
-
- (let ((*print-readably* t))
- (format nil "~%~s" (make-instance 'foo :foo2 4)))
-
- => "
-
- #.(make-instance 'FOO
- :foo1 1
- :foo2 4 )"
- |#
-