Subject: Re: macro question...
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 22 Apr 2003 06:23:46 -0500
Newsgroups: comp.lang.lisp
Message-ID: <RLadnUlyAfrfuzijXTWc-g@speakeasy.net>
Peter Seibel  <peter@javamonkey.com> wrote:
+---------------
| Doug Philips <dgou@mac.com> writes:
| > What I want to have 'invokeMacro' do is both:
| >       a) return its arguments (except for the 1st one)....
| > AND   b) stash a closure of the code that is its arguments...
...
|     (defmacro invoke-macro (tag &body body)
|       `(progn
|         (setf (gethash ,tag *captured-code*) #'(lambda () ,@body))
|         ,@body))
+---------------

If the bodies can be very large, rather than duplicating the code
one might prefer to do something like this:

	(defmacro invoke-macro (tag &body body)
	  (let ((thunk (gensym)))
	    `(flet ((,thunk () ,@body))
	       (setf (gethash ,tag *captured-code*) #',thunk)
	       (,thunk))))

But in any case, be aware that each time control passes through
(the expansion of) INVOKE-MACRO you're going to overwrite the
save-away code with a new version, so if you do this inside of
a loop you will only save the closure over the last iteration
of the body.


-Rob

-----
Rob Warnock, PP-ASEL-IA		<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607