unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Why is Elisp's defvar weird? And is eval_sub broken?
@ 2015-02-12 21:32 Kelly Dean
  2015-02-13 19:03 ` Stefan Monnier
  0 siblings, 1 reply; 21+ messages in thread
From: Kelly Dean @ 2015-02-12 21:32 UTC (permalink / raw)
  To: emacs-devel

desktop.el has these top-level definitions:
(defvar desktop-first-buffer)
(defvar desktop-buffer-ok-count)
(defvar desktop-buffer-fail-count)

The docstring for defvar says:
⌜Define SYMBOL as a variable, and return SYMBOL.
...
The `defvar' form also declares the variable as "special",
so that it is always dynamically bound even if `lexical-binding' is t.
...
If INITVALUE is missing, SYMBOL's value is not set.

If SYMBOL has a local binding, then this form affects the local
binding.⌝

But that's wrong. If INITVALUE is missing, and lexical-binding is t (as is the case in desktop.el), then not only is the value not set, but also the variable is _not_ declared special, even if the defvar is at top level.

That means that even after loading desktop.el, if you let-bind the three variables above in a function defined in a file other than desktop.el, and lexical-binding is t in that other file, then those variables will be bound lexically, not dynamically.

This was causing an inexplicable bug in my code that uses functions from desktop.el, that I could figure out how to fix only by using setq instead of «let» for those variables. Then today I happened to read the source code for defvar and discovered what's really going on. I can fix my bug without setq, by instead using defvar (without INITVALUE) before «let». If the docstring for defvar were true (i.e. top-level defvar declares special (even if INITVALUE is missing)), then I wouldn't need to use defvar before the «let», because desktop.el would have already made those variables special. It's no problem for me to use defvar before the «let», but the docstring should say what defvar really does, so people know it's necessary in this case.

Also, CL doesn't behave this way. E.g. In Elisp (in Emacs 24.4):
(setq lexical-binding t)
(let ((foo 'bar)) (defvar foo) foo) → bar
(let ((foo 'baz)) (makunbound 'foo) foo) → baz

But in CL:
(let ((foo 'bar)) (defvar foo) foo) → bar
(let ((foo 'baz)) (makunbound 'foo) foo) → error: foo is unbound

In Elisp, both let-bindings are lexical, but in CL, the second let-binding is dynamic.

What's the purpose of Elisp behaving this way? Is it just to enable local use of dynamic variables (for implicit arguments and return values of functions) without having to clutter the globals with otherwise-unneeded special variables? If so, a cleaner way to do it would be with a dynamic-let special form, rather than a weirdly-behaving defvar.

Also, in Elisp:
(setq lexical-binding t)
(let ((foo 0))
  (defvar foo)
  (let ((foo 1))
    foo)) → 0

That's because eval_sub in eval.c looks up the variable in the lexical environment using only Fassq, without first using Fmemq to check for a local dynamic binding. Is that behavior actually correct?



^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2015-02-28 10:15 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-12 21:32 Why is Elisp's defvar weird? And is eval_sub broken? Kelly Dean
2015-02-13 19:03 ` Stefan Monnier
2015-02-14  7:35   ` Kelly Dean
2015-02-14 14:36     ` Stefan Monnier
2015-02-15 14:17       ` Daniel Colascione
2015-02-16  5:42       ` Kelly Dean
2015-02-16  7:40         ` Stefan Monnier
2015-02-17 23:39           ` Kelly Dean
2015-02-18 22:29             ` Stefan Monnier
2015-02-19 10:32               ` Kelly Dean
2015-02-19 13:23                 ` Stefan Monnier
2015-02-20  0:11                   ` Kelly Dean
2015-02-20  2:02                     ` Stefan Monnier
2015-02-22  4:11                       ` Proposal for a closed-buffer tracker Kelly Dean
2015-02-22 15:53                         ` Eli Zaretskii
2015-02-22 22:03                           ` Stefan Monnier
2015-02-22 22:23                             ` Dmitry Gutov
2015-02-23 13:53                               ` Artur Malabarba
2015-02-23 16:44                                 ` Eli Zaretskii
2015-02-22 21:59                         ` Stefan Monnier
2015-02-28 10:15                         ` Artur Malabarba

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).