Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can macro expansion contain (declare ...) expressions?

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?

like image 335
nbtrap Avatar asked Sep 05 '13 20:09

nbtrap


1 Answers

Your first example is exactly what it's prohibiting. You couldn't have code like that combined with something like this:

(defun optimized (a b)
  (optimize-fully)
  (+ a b))

I sometimes see code like this, though:

(defvar *optimization-settings* '(optimize (speed 3) (safety 0)))

(defun foo (a b)
  (declare #.*optimization-settings*)
  ...)
like image 52
Xach Avatar answered Sep 22 '22 16:09

Xach