Closed swsnr closed 10 years ago
Thanks, lexical binding can go in the bottom of the file. See below for a test. The other changes look good so I'll merge it.
(setq test (let ((foo "bar"))
(lambda ()
(message foo))))
;; Shows "bar" if lexical-binding is enabled, show "something-else" for dynamic binding.
(let ((foo "something-else"))
(funcall test))
;; Local Variables:
;; lexical-binding: t
;; End:
@jschaf With respect, did you think that I just made this up?
See the documentation of lexical-binding
in Using Lexical Binding (emphasis mine):
If this buffer-local variable is non-nil, Emacs Lisp files and buffers are evaluated using lexical binding instead of dynamic binding. (However, special variables are still dynamically bound; see below.) If nil, dynamic binding is used for all local variables. This variable is typically set for a whole Emacs Lisp file, as a file local variable (see File Local Variables). Note that unlike other such variables, this one must be set in the first line of a file.
Likewise, the docstring of this variable (emphasis mine):
Whether to use lexical binding when evaluating code. Non-nil means that the code in the current buffer should be evaluated with lexical binding. This variable is automatically set from the file variables of an interpreted Lisp file read using `load'. Unlike other file local variables, this must be set in the first line of a file.
Also, loading a file with lexical binding set in file local variables at the end of the file pops up a corresponding warning.
Your example works by accident, probably because lexical-binding
was enabled in some other way, or because it is too short to trigger misbehaviour with lexical-binding
at the end of the file.
No, not at all. I spoke too soon. I went back and tested it and you're right. Thanks for the detailed description.
Lexical binding must be at the beginning of a file.