What is going on
From the "theoretical" (Scheme/Common Lisp) point of view, as soon as you enable lexical bindings, for all practical purposes alice-multiplier-1
and alice-multiplier-2
are identical. Any difference in their behavior is a bug in Emacs Lisp and should be reported as such.
Compiled
If you put your code (i.e., the 2 defun
s and the ;; -*- lexical-binding: t; -*-
line) into a file, emacs-list-byte-compile-and-load
it, then you can test my claim by evaluating these 4 forms:
(disassemble 'alice-multiplier-1)
(disassemble 'alice-multiplier-2)
(disassemble (alice-multiplier-1 10))
(disassemble (alice-multiplier-2 10))
you will see that 3 and 4 are identical and 1 and 2 differ by one instruction (which should be reported as a bug to the Emacs maintainers, but does not affect the behavior).
Note that none of the disassemblies mention foo
, which means that the defvar
will not affect their behavior.
All is good!
Interpreted
Indeed, the behavior you see is incorrect; the correct result after defvar
is
(:R1 (10000 20000 30000) :R2 (10000 20000 30000))
please report this to the emacs maintainers.
Different???
Yes, defvar
does (and should!) affect the behavior of interpreted code and does not (and should not!) affect the behavior of compiled code.
What you should do
There is no way to "protect" your foo
from being proclaimed special
by others - except by prefixing "your" symbols with alice-
.
However, if you byte-compile the file with the alice-multiplier-1
definition, the compiled file does not even contain foo
and thus future declarations of foo
will not affect you.