|
From: | Dave Goel |
Subject: | bug#66940: Dynamic scoping is all weird now? |
Date: | Mon, 6 Nov 2023 01:57:05 -0500 |
Dave Goel <deego3@gmail.com> writes:
> (progn
> (setq lexical-binding nil)
>
> (dotimes (ii 10)
> (defmacro mac ()
> `(message "%S" ,ii)
> )
> (let
> ((old_ii ii))
> (setq ii 33)
> (mac)
> (setq ii old_ii)
> )))
This code has two problems:
(1) Setting `lexical-binding' in the middle of an evaluation does not
work as you think. Either you eval en _expression_ (like the above) using
lexical binding or dynamical binding. If you change `lexical-binding'
in the middle you might get unexpected behavior. Please set the binding
mode only in the file header, not in the code. In the extremely rare
cases where you really must evaluate an _expression_ using the other
binding mode use `eval' with an appropriate second argument.
(2) Macro expansion does not work as you think. Most of the time macros
are expanded _once_ in the complete _expression_ and then the result is
evaluated. When you redefine a macro in the middle of evaluating code
using it, most of the time this will not have an effect because the
macro had already been expanded in the following code.
> You eval this code once. It works.
> You eval this again. It works.
> The third time, though, it lands you in the debugger. The very same
> code. Why the third time? And, why the debugger? ii is well set every
> time it is used.
It's not surprising.
The first time you start evaluating the _expression_ using lexical
binding. Because your code sets lexical-binding to nil, the second time
you actually use dynamical binding completely.
The third run fails because when `mac' is expanded a variable `ii' is
undefined on top-level, so macroexpansion fails.
The second run is different: because the first run had used
lexical-binding (more or less completely, since you started the
interpreter using lexical-binding mode), the macroexpander of `mac' is
actually a closure that inherited the value of `ii' from the last
iteration of the first run. Because of that the second run succeeds.
The second run redefines `mac' so that its reference to `ii' now means
the dynamical global variable, which doesn't exist.
So, I think everything indeed perfectly works as expected here.
Michael.
[Prev in Thread] | Current Thread | [Next in Thread] |