Comment 2 for bug 632368

Nikodemus Siivola (nikodemus) wrote :

Better version:

* Augment the environment for the compiler macro to look at.

* Only try once per number of constants available.

Can handle this like this without breaking a sweat:

(defun foo (x)
  (format nil "foo=~S" x))

(defparameter *foo-count* 0)

(define-compiler-macro foo (&whole form x)
  (if (constantp x)
        (incf *foo-count*)
        (foo x))

(declaim (inline bar))
(defun bar (x &key car)
  (list car (foo x)))

(declaim (inline quux))
(defun quux (x y)
  (when y x))

(defun zot ()
  (let ((x (quux "ding" t)))
    (bar (quux x (quux t t)) :car '=)))

(assert (= 1 *foo-count*))