Frank Buss <fb@frank-buss.de> wrote:
+---------------
| Edi Weitz <edi@agharta.de> wrote:
| > 1. Don't use top-level SETQ because this is asking for undefined
| > behaviour. Use DEFPARAMETER or DEFVAR instead.
|
| ok, if I understand the CL standard correctly, SETF (and SETQ) does a
| lexical binding, like LET, but DEFVAR and DEFPARAMETER a special binding.
+---------------
*NO!* SETF/SETQ don't do binding at all! They only *mutate* an existing
variable binding, which can be either lexical or special (depending on
what bindings & declarations are in scope at the point SETF/SETQ are used).
+---------------
| In CMUCL I got a warning that a variable set with SETQ is declared
| special, so I assume it doesn't make sense to make a lexical variable
| binding in global namespace, but I don't understand it fully.
+---------------
The Common Lisp standard simply does not specify any way to create a
"global lexical variable", sorry. Specifically, the lexical environment
is disjoint from the global environment. This may be somewhat confusing
given that CLHS "3.1.1.3 Lexical Environments" says:
Within a given namespace, a name is said to be bound in a lexical
environment if there is a binding associated with its name in the
lexical environment or, if not, there is a binding associated with
its name in the global environment.
But CLHS "3.1.1.1 The Global Environment" makes it clear [by explicitly
listing everything that the global environment includes] that the global
environment does not contain any lexical bindings of variables, or rather,
as CLHS "3.1.1.3.1 The Null Lexical Environment" puts it:
The null lexical environment is equivalent to the global environment.
*Some* implementations might allow you to SETF/SETQ a variable without
binding or declaring it (particularly in a top-level form in the REPL),
and might (or might not) thereafter treat that variable as a "global
lexical" in the absence of any later global proclamations... but you
can't count on that.
-Rob
p.s Note that much of the effect one typically wants from a "global
lexical variable" can be achieved with some careful DEFINE-SYMBOL-MACRO
macrology.
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607