Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!nstn.ns.ca!wrdis01!mips!sdd.hp.com!wuarchive!uunet!stanford.edu!lucid.com!karoshi!eb From: eb@lucid.com (Eric Benson) Newsgroups: comp.lang.lisp Subject: Re: shadowing defstruct print-function Message-ID: Date: 31 May 91 20:27:22 GMT References: <1991May31.183953.29579@jpl-devvax.jpl.nasa.gov> Sender: usenet@lucid.com Organization: Lucid, Inc., Menlo Park, CA Lines: 40 In-Reply-To: charest@ai-cyclops.jpl.nasa.gov's message of 31 May 91 18:39:53 GMT In article <1991May31.183953.29579@jpl-devvax.jpl.nasa.gov> charest@ai-cyclops.jpl.nasa.gov (Len Charest) wrote: > Is it possible to shadow the print-function used to print a > structure during a call to FORMAT? For example, suppose we have a > structure FOO that prints using the print-function PRINT-FOO, i.e.,: > (format t "~a" foo) > => # > Now I want to locally redefine PRINT-FOO as in: > (flet ((print-foo (foo stream depth) > (declare (ignore foo depth)) > (format stream "{Foo}"))) > (format t "~a" foo)) > => {Foo} > execpt that FLET only has lexical scope and the ~a FORMAT directive > calls PRINT-FOO at a nested level where the global definition is > active. So # will really be printed. Any pointers? The simplest way to do this is: (defstruct (foo (:print-function print-foo)) ) (defvar *print-foo-hook* #'(lambda (foo stream depth) )) (defun print-foo (foo stream hook) (funcall *print-foo-hook* foo stream hook)) Then your local redefinition is: (let ((*print-foo-hook* #'(lambda (foo stream depth) (declare (ignore foo depth)) (format stream "{Foo}")))) (format t "~a" foo)) eb@lucid.com Eric Benson 415/329-8400 x5523 Lucid, Inc. Telex 3791739 LUCID 707 Laurel Street Fax 415/329-8480 Menlo Park, CA 94025