Reputation: 10573
Here is the snippet that confuses me:
(setq lexical-binding t)
(defvar x 0)
(setq test (let ((x 1))
(lambda ()
x)))
(funcall test)
My understanding is that since lexical-binding
is true, then the x of value 1 should cover the scope of let
, which should include the x
in the definition of lambda
, as such, the test should return value of 1 instead of 0, but it turns out to return 0, which is the value of x
by defvar
.
Did I misunderstand anything?
UPDATE
Just for clarification, I would like to put my understanding here. Dynamical bounding means it only have one symbol and the value is popped in and out in a stack. As such, when the definition of lambda
is done, the value used in let is popped out.
lexical/static bounding means the value is always been checked in the context of the lexical environment, so as long there is let
before lambda
definition, the value in let
is used.
variable defined by defvar
is always dynamically bound, as such, lexical-binding control here does not make any difference.
Upvotes: 3
Views: 174
Reputation: 435
According to https://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Lexical-Binding.html, even when lexical-binding
is non-nil
, special variables (like x
since it was defined with defvar
) are still dynamically bound.
Upvotes: 3