I had a bug in one of my extensions that eventually turned out to be caused by set not working as I expected:

;; -*- lexical-binding: t -*-

(let ((a nil))
  (setq a t)
  (print a))

(let ((a nil))
  (set 'a t)
  (print a))

when run with emacs -Q --batch -l temp.el prints:



This seems very strange to me. I was under the impression that (setq a b) is shorthand for (set 'a b). What's going on?


This is documented behaviour. The (much improved) explanation in the Emacs 25.1 elisp manual is as follows:

Note that unlike dynamic variables which are tied to the symbol object itself, the relationship between lexical variables and symbols is only present in the interpreter (or compiler). Therefore, functions which take a symbol argument (like ‘symbol-value’, ‘boundp’, and ‘set’) can only retrieve or modify a variable’s dynamic binding (i.e., the contents of its symbol’s value cell).

C-hig (elisp) Lexical Binding


BTW, one way to think about why it can't work is to remember that lexical scoping enjoys the so-called "α-renaming" property: variable names do not matter, and you (or the compiler) can trivially (i.e. without having to understand what the code does) rename a variable (as long as the new name doesn't collide with some other local variable) without changing the meaning of the code (and hence the result of evaluation).

Now how could that work with code like:

(let ((a nil)
      (varname 'a))
  (set varname t)
  (message "%s = %s" varname a))

If you expect this to say a = t, then how could the compiler rename the variable to fresh-a and still get the same output?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.