The Common Lisp Hyperspec states "Macro forms cannot expand into declarations; declare expressions must appear as actual subexpressions of the form to which they refer."
I'm confused on the meaning of "expand into". A macro such as the following won't work for obvious reasons:
(defmacro optimize-fully ()
`(declare (optimize (speed 3) (safety 0))))
But what if the macro expansion merely contains a (declare ...)
expression?
(defmacro defun-optimized (name (&rest lambda-list) &rest body)
`(defun ,name ,lambda-list
(declare (optimize (speed 3) (safety 0)))
,@body))
(defun-optimized foobar (a b)
(* a b))
Is this a violation of the spec? The CL implementation I use, SBCL, does not complain, and in fact, the macro above seems to work precisely as expected. What gives?
Your first example is exactly what it's prohibiting. You couldn't have code like that combined with something like this:
I sometimes see code like this, though: