home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / lisp / 2345 < prev    next >
Encoding:
Internet Message Format  |  1992-08-31  |  2.4 KB

  1. Path: sparky!uunet!sun-barr!cs.utexas.edu!sdd.hp.com!think.com!barmar
  2. From: barmar@think.com (Barry Margolin)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: static, a.k.a. eval-once
  5. Date: 31 Aug 1992 21:22:28 GMT
  6. Organization: Thinking Machines Corporation, Cambridge MA, USA
  7. Lines: 61
  8. Message-ID: <17u2ikINNs6g@early-bird.think.com>
  9. References: <7399@skye.ed.ac.uk>
  10. NNTP-Posting-Host: telecaster.think.com
  11.  
  12. In article <7399@skye.ed.ac.uk> jeff@aiai.ed.ac.uk (Jeff Dalton) writes:
  13. >About 4 years ago, I wrote an EVAL-ONCE macro.  It appears, with
  14. >an explanation below.  It has been claimed that my macro isn't
  15. >portable because the behavior of gensyms isn't sufficiently
  16. >restricted when compile and load is involved.  What I want to
  17. >know is:
  18. >
  19. >  1. Is there a problem with using GENSYM this way?
  20.  
  21. I think it should work.  While the compiler has to arrange for all
  22. references to a particular gensym within a binary file to be the object,
  23. similarly named gensyms in different binary files should refer to different
  24. instances.
  25.  
  26. BTW, I assume you realize that V should be a gensym, i.e. it should be:
  27.  
  28. I think your version can be simplified a bit.  The expansion doesn't need a
  29. LET binding:
  30.  
  31. (defmacro eval-once (form)
  32.   (let ((var (gensym)))
  33.     `(if (boundp ',var)
  34.      (symbol-value ',var)
  35.      (set ',var ,form))))
  36.  
  37. >  2. Is there a better way to get what I want.
  38.  
  39. Not that I can think of quickly (I thought of using something like Scheme's
  40. delay/force, but it didn't work).
  41.  
  42. This looks suspiciously like the stuff we were discussing a few months ago,
  43. where you wanted the ability to have modifiable literals in your program.
  44. If that were allowed, you could do:
  45.  
  46. (defstruct eval-once
  47.   (done nil)
  48.   value)
  49.  
  50. (defmacro eval-once (form)
  51.   (let ((struct (make-eval-once)))
  52.     `(if (eval-once-done ',struct)
  53.       (eval-once-value ',struct)
  54.       (setf (eval-once-done ',struct) t
  55.         (eval-once-value ',struct) ,form)))))
  56.  
  57. >  3. Is there a way that will get the EVAL-ONCE effect in
  58. >     interpreted code even in an "expand macros each time"
  59. >     implementation?  (N.B. There is no correct portable way
  60. >     to use *macroexpand-hook* cache macro expansions --
  61. >     see page 204 of CLtL II.)
  62. ...
  63. >For the interpreted code problem, I can't think of anything better
  64. >than having EVAL-ONCE assume that EQ forms are never logically
  65. >different expressions.
  66.  
  67. I can't think of another way, either.
  68. -- 
  69. Barry Margolin
  70. System Manager, Thinking Machines Corp.
  71.  
  72. barmar@think.com          {uunet,harvard}!think!barmar
  73.