From: Dmitry Gutov <dgutov@yandex.ru>
To: help-gnu-emacs@gnu.org
Subject: What's the difference between (defvar foo) and (declare (special foo)), with lexical-binding on?
Date: Sun, 27 Jul 2014 06:32:44 +0300 [thread overview]
Message-ID: <53D472DC.7070405@yandex.ru> (raw)
[-- 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)
next reply other threads:[~2014-07-27 3:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-27 3:32 Dmitry Gutov [this message]
2014-07-27 8:16 ` What's the difference between (defvar foo) and (declare (special foo)), with lexical-binding on? Stefan Monnier
2014-07-27 9:00 ` Dmitry
2014-07-27 9:58 ` Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=53D472DC.7070405@yandex.ru \
--to=dgutov@yandex.ru \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.