home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.lisp
- Path: sparky!uunet!decwrl!elroy.jpl.nasa.gov!ufo!Aig.Jpl.Nasa.Gov!charest
- From: charest@Aig.Jpl.Nasa.Gov (Len Charest)
- Subject: Re: Function to print in #S format
- Message-ID: <1992Sep9.011022.14516@jpl-devvax.jpl.nasa.gov>
- Followup-To: comp.lang.lisp
- Sender: usenet@jpl-devvax.jpl.nasa.gov (For NNTP so rrn will be able to post)
- Nntp-Posting-Host: ai-cyclops
- Reply-To: charest@aig.jpl.nasa.gov
- Organization: NASA/Jet Propulsion Laboratory
- References: <SJAMESON.92Sep8161926@fergie.dnet.ge.com>
- Date: Wed, 9 Sep 1992 01:10:22 GMT
- Lines: 56
-
- In article <SJAMESON.92Sep8161926@fergie.dnet.ge.com>, sjameson@fergie.dnet.ge.com (Stephen M Jameson) writes:
- |> I would like to have a :PRINT-FUNCTION
- |> specified in the DEFSTRUCT which will print in a concise, non-readable format
- |> if a certain switch is set, and print in the verbose, readable format
- [i.e., #S syntax]
- |> if the switch is not set.
-
- The following long-winded reply assumes that you are really trying to
- solve the following two problems: switch between concise and verbose
- printed representations of structures; and ensure a printed
- representation such that "the reader will automatically reconstruct the
- structure".
-
- There is no one variable or function built into CL that will provide
- this behavior. DEFSTRUCT does not provide a handle on the #S structure
- printer that the Lisp implmentation is required to have. The #S printer
- returns "whatever object the constructor macro [for the structure]
- returns" (CLtL2, p. 537) *not* an object that is EQ to the object being
- printed. And the situation is further complicated by the new global
- variable *PRINT-READABLY*, which when it's value is TRUE (or maybe
- non-NIL) then "printing any object must ... produce a printed
- representation that the reader will accept [and] ... on reading the
- printed representation, produce an object that is 'similar as a
- constant' to the object that was printed." (CLtL2, p 557) The #S
- printer and *PRINT-READABLY* would then seem to be at odds with one
- another.
-
- Section 25.1.4 explains the notion of 'similar as a constant' and
- suggests that MAKE-LOAD-FORM be used for constructing such objects at
- *load time*.
-
- Some suggestions:
-
- 1. Define your own switch *PRINT-CONCISELY* and make all of your
- structure print-functions obey it. Yes, this means you must explicitly
- check the switch in your code, but you may exploit
- PRINT-UNREADABLE-OBJECT to handle printing when *PRINT-CONCISELY* is
- non-NIL (probably the default value). Also, PRINT-UNREADABLE-OBJECT
- obeys *PRINT-READABLY* as an added bonus.
-
- 2. If you really want structures to print as #S( ... ) when your switch
- is NIL (not set) then you have to maintain the names of the slots of
- the structure yourself. Yes, it looks like the implementation must
- already maintain that info, since #S itself prints the names of the
- slots, but no handle on the slot-name info is provided. (You could
- write code that had read-time conditionals for specific Lisp
- implementations, but in your post you mentioned you were worried about
- portability.) As an alternative you could define your own # macro
- character that accepted positional arguments for structure slot values
- instead of keyword arguments.
-
- 3. (mutually exclusive) Use CLOS. Ha Ha only serious.
- ..................................................
- Len Charest, Jr.
- JPL Artificial Intelligence Group
- charest@aig.jpl.nasa.gov
-