Michael, Many thanks for that excellent explanation. One more question - With this, first, let's simplify our example as follows. Let's eval this at the start: (setq lexical-binding nil) Eval it, so we are operating in the old world of dynamic scoping. (let ((ii 1)) (defmacro mac () `(print ,ii) ) (mac)) As you said, with lexical-binding nil, the ii = 1 had no effect above. It will use the ii from the runtime environment. So, let's try this - (setq ii 3) (let ((ii 4)) (mac)) It evals to 3, not 4. Per our understanding of dynamic scoping, we would have expected it use the innermost ii? Why does it use the global ii? On Sun, Nov 5, 2023 at 8:52 PM Michael Heerdegen wrote: > Dave Goel 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. >