What's happening
In terms of โtheoreticalโ (schema / generic Lisp), as soon as you turn on lexical bindings, for all practical purposes, alice-multiplier-1 and alice-multiplier-2 identical . Any difference in their behavior is a bug in Emacs Lisp and should be reported as such.
Compiled by
If you put your code (i.e. line 2 defun and ;; -*- lexical-binding: t; -*- ) in a file, emacs-list-byte-compile-and-load it, you can check my requirement 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 in one instruction (which should be reported as an error for Emacs-enabled ones, but does not affect the behavior).
Note that none of the disassembly mentions foo , which means that defvar will not affect their behavior.
Things are good!
Interpreted
In fact, the behavior you see is incorrect; the correct result after defvar is
(:R1 (10000 20000 30000) :R2 (10000 20000 30000))
please inform emacs developers about this .
Various???
Yes, defvar does (and should!) Influence the behavior of the interpreted code and does not (and should not!) Influence the behavior of the compiled code.
What you should do
It is not possible to "protect" your foo from declaring special others - except that the prefix "your" characters is denoted by alice- .
However, if you byte-compile a file with the definition alice-multiplier-1 , the compiled file does not even contain foo , and therefore future foo declarations will not affect you.