all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / 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

* Re: What's the difference between (defvar foo) and (declare (special foo)), with lexical-binding on?
  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
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2014-07-27  8:16 UTC (permalink / raw
  To: help-gnu-emacs


(declare (special foo))

is a hack defined in CL.  It predates lexical-binding and I don't
think anyone has tried to adapt it to lexical-binding, so I don't know
what it does in the case of lexical-binding.


        Stefan




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

* Re: What's the difference between (defvar foo) and (declare (special foo)), with lexical-binding on?
  2014-07-27  8:16 ` Stefan Monnier
@ 2014-07-27  9:00   ` Dmitry
  2014-07-27  9:58     ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry @ 2014-07-27  9:00 UTC (permalink / raw
  To: Stefan Monnier; +Cc: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> (declare (special foo))
>
> is a hack defined in CL.  It predates lexical-binding and I don't
> think anyone has tried to adapt it to lexical-binding, so I don't know
> what it does in the case of lexical-binding.

Thanks! So, you would advise not to use it in lexical-binding code at
all?

I think the biggest problem with it is it sorta-works-but-not-exactly.



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

* Re: What's the difference between (defvar foo) and (declare (special foo)), with lexical-binding on?
  2014-07-27  9:00   ` Dmitry
@ 2014-07-27  9:58     ` Stefan Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2014-07-27  9:58 UTC (permalink / raw
  To: help-gnu-emacs

> Thanks! So, you would advise not to use it in lexical-binding code at all?

I think so, yes.


        Stefan




^ 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

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.