david.tolpin@gmail.com <david.tolpin@gmail.com> wrote:
+---------------
| So, how can I bind a variable in the top-level scope without
| declaring it special?
+---------------
You can't (not portably). Read CLHS "3.1.1.1 The Global Environment",
where it says what the global environment contains. Note that lexical
variables are conspicuously absent.
Several people have addressed this with macros (named variously
DEFLEX or DEFLEXICAL) that use DEFINE-SYMBOL-MACRO to do "indirection"
on what appears to be a top-level binding, while allowing lexical
rebinding in nested scopes... which is really what most people want
from top-level lexicals anyway. E.g.:
> (deflex foo 13)
FOO
> (defun foo-val () foo)
FOO-VAL
> (let ((foo 27))
(list foo (foo-val)))
(27 13)
>
Here's what one implementation [mine] of DEFLEX generates:
> (macroexpand '(deflex foo 13))
(PROGN
(DEFPARAMETER *STORAGE-FOR-DEFLEX-VAR-FOO* 13)
(DEFINE-SYMBOL-MACRO FOO *STORAGE-FOR-DEFLEX-VAR-FOO*))
T
> (macroexpand '(deflex bar 54 "A top-level lexical"))
(PROGN
(DEFPARAMETER *STORAGE-FOR-DEFLEX-VAR-BAR* 54 "A top-level lexical")
(SETF (DOCUMENTATION 'BAR 'VARIABLE) "A top-level lexical")
(DEFINE-SYMBOL-MACRO BAR *STORAGE-FOR-DEFLEX-VAR-BAR*))
T
>
-Rob
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607