Re: lambda-returning defmacro, capture

Juho Snellman <jsnell@xxxxxx> writes:

Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx> wrote:
Well, right, but (setf (symbol-value 'sym) val) and (symbol-value 'sym)
seem to be defined independently of the different kind of variable.

So you're right that using sym to access (symbol-value 'sym) in the
case of a non dynamic variable is not specified, but we can use

But why in the world would anyone do that, if the goal is to get
"global lexicals"? There's a good and simple way to accomplish that,
by using symbol-macros.

By the way, I don't find a clear specification that SYMBOL-VALUE
should access the value of dynamic variables... There's some weak
correlation of the two thru boundp, but that's all I can find.

See the description of the SYMBOL class:


If a symbol has a value attribute, it is said to be bound, and
that fact can be detected by the function boundp. The object
contained in the value cell of a bound symbol is the value of the
global variable named by that symbol, and can be accessed by the
function symbol-value. A symbol can be made to be unbound by the
function makunbound.

Ah right. And global variables are either dynamic variables or
constant variables.


Operations on a symbol's value cell and function cell are sometimes
described in terms of their effect on the symbol itself, but the
user should keep in mind that there is an intimate relationship
between the contents of those cells and the global variable or
global function definition, respectively.

Ok so (setf (symbol-value 'sym) val) really binds a dynamic variable,
but it doesn't declare it special in the global or any environment.

So evaluating sym in the global environment accesses the dynamic
variable because it is in the global environment, and
(funcall (let ((sym 'lex-val)) (lambda () sym)))
returns lex-val since the lambda use the lexical binding of sym.

And similarly for the (locally (declare (special sym) (setf sym val))).

And then, SET and SETQ don't do the same thing, but archieve the same
SET is equivalent to (setf (symbol-value sym) val)
while SETQ can magically access the dynamic variable in the global
environment even when it's not declared special!

__Pascal Bourguignon__

"What is this talk of "release"? Klingons do not make software
"releases". Our software "escapes" leaving a bloody trail of
designers and quality assurance people in its wake."