14

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:

t

nil

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?

14

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

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.