I've been reading about emacs dynamic and lexical bindings. While I generally get the difference between the two types, there is one example that is not clear to me.
I've checked this question and I think I understand why the second example prints nil instead of t
(I've read about set
and setq
when lexical-binding: t
;; -*- lexical-binding: t -*-
(let ((a nil))
(setq a t)
(print a))
(let ((a nil))
(set 'a t)
(print a))
From the elisp manual I've read that when lexical-binding is in effect set
affects the dynamic value of a variable where as setq
affects its current lexical value.
However from the following example it looks like (setq a 5)
sets the dynamic value of a since later (set 'a t) changes top level a
.
;;; -*- lexical-binding: t; -*-
(setq a 5)
(let ((a nil))
(setq a t)
(print a)) ;; prints t
(let ((a nil))
(set 'a t)
(print a)) ;; prints nil
a ;; prints t (I've expected this to be unchanged i.e. 5)
My question is when lexical-binding is on, what is the binding for the top level variables ?