18

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?

Drew
  • 75,699
  • 9
  • 109
  • 225
dshepherd
  • 1,281
  • 6
  • 18

2 Answers2

17

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

phils
  • 48,657
  • 3
  • 76
  • 115
6

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?

Stefan
  • 26,154
  • 3
  • 46
  • 84