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

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