unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* What's the difference between (defvar foo) and (declare (special foo)), with lexical-binding on?
@ 2014-07-27  3:32 Dmitry Gutov
  2014-07-27  8:16 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Gutov @ 2014-07-27  3:32 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 771 bytes --]

1. Save a.el and b.el to the same dir.

2. Open b.el, type M-x eval-buffer.

3. Evaluate (b-foo), see 4 in the echo area.

Note that the assertion in `b-bar' succeeds, and so while `a-a' is not 
globally bound, it's visible from `b-bar'.

If I replace the definition of b-bar with

```
(defvar a-a)

(defun b-bar ()
   (cl-assert (= a-a 4))
   (let ((a-a 5))
     (a-bar)))
```

or even with

```
(defun b-bar ()
   (defvar a-a)
   (cl-assert (= a-a 4))
   (let ((a-a 5))
     (a-bar)))
```

(which apparently doesn't make `a-a' globally defined in that file, ref. 
http://debbugs.gnu.org/18059)

then, as I'd expect, after M-x eval-buffer, (b-bar) evaluates to 5.

Why does this happen? Is (defvar foo) the recommended option to use with 
lexical-binding on?

-- Dmitry

[-- Attachment #2: a.el --]
[-- Type: text/x-emacs-lisp, Size: 118 bytes --]

(defvar a-a)

(defun a-foo (callback)
  (let ((a-a 4))
    (funcall callback)))

(defun a-bar ()
  a-a)

(provide 'a)

[-- Attachment #3: b.el --]
[-- Type: text/x-emacs-lisp, Size: 222 bytes --]

;;; -*- lexical-binding: t -*-

(let ((load-path '(".")))
  (require 'a))

(defun b-foo ()
  (a-foo #'b-bar))

(defun b-bar ()
  (declare (special a-a))
  (cl-assert (= a-a 4))
  (let ((a-a 5))
    (a-bar)))

(provide 'b)

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

end of thread, other threads:[~2014-07-27  9:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-07-27  3:32 What's the difference between (defvar foo) and (declare (special foo)), with lexical-binding on? Dmitry Gutov
2014-07-27  8:16 ` Stefan Monnier
2014-07-27  9:00   ` Dmitry
2014-07-27  9:58     ` Stefan Monnier

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).