Subject: Re: hanging macros
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 25 May 2004 18:42:56 -0500
Newsgroups: comp.lang.lisp
Message-ID: <B_KdnQhzlp2dRy7dRVn-uA@speakeasy.net>
Alain Picard  <Alain.Picard@memetrics.com> wrote:
+---------------
| "Coby Beck" <cbeck@mercury.bc.ca> writes:
| > I have delibrately ignored eval-when but I have a feeling it may
| > actually be there for a reason and one I now need to understand.
...
| The basic way to think about this is: if you have 2 macros
| in a file, say A and B, and the compiler needs to invoke
| A to compile the macroexpansion code of B, A should be
| surrounded in an eval-when.
+---------------

As Duane and others have said, that's now correct. But where you *do*
need EVAL-WHEN is when macro A is in a *file* that is LOADed (or some
other means, such as REQUIREd) at compile time, e.g.:

	(require :foo)	; "foo.lisp" contains (defmacro A ...)
	...
	(defmacro B ...uses A...)
	...
	(A args...)	; or even just a bare use of A!

These will fail. The following will succeed:

	(eval-when (:compile-toplevel :load-toplevel :execute)
	   (require :foo))	; "foo.lisp" contains (defmacro A ...)
	...
	(defmacro B ...uses A...)
	...
	(A args...)	; or even just a bare use of A!

More commonly, I find myself using this simpleminded pattern in
my ASDF files, for modules that don't have ASDF subsystem names:

	;;; for macros/packages
	(eval-when (:compile-toplevel :load-toplevel :execute)
	  (require :foo)
	  (require :bar)
	  (require :baz))
	  (require :util

	(defpackage :org.rpw3.some-pkg
	  (:use :cl :ext :foo :bar :baz))

	(defpackage #:org.rpw3.some-pkg.system (:use :cl :asdf))
	(in-package #:org.rpw3.some-pkg.system)

	(defsystem some-pkg
	   ...usual ASDF stuff...)


-Rob

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