all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Odd behavior of `eval-when-compile'
@ 2007-07-08  5:25 Jay Belanger
  2007-07-08 18:51 ` Stefan Monnier
  0 siblings, 1 reply; 3+ messages in thread
From: Jay Belanger @ 2007-07-08  5:25 UTC (permalink / raw)
  To: emacs-devel; +Cc: jay.p.belanger


I came across the following surprising (to me) behavior for
`eval-when-compile'.  Suppose I have two files:
a.el, containing
   (defvar vara 2)
   (provide 'a)
and b.el, containing
   (require 'a)
   (defvar varb (eval-when-compile (+ 1 vara)))
If I byte compile b.el, it works fine.
However, if I have a file b.el containing
   (defvar vara 2)
   (defvar varb (eval-when-compile (+ 1 vara)))
and then byte compile it (in a new Emacs session), I get an error
    Symbol's value as variable is void: vara
Is there a reason for this behavior (I didn't find it in the elisp
manual), or is this a bug?

Jay

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

* Re: Odd behavior of `eval-when-compile'
  2007-07-08  5:25 Odd behavior of `eval-when-compile' Jay Belanger
@ 2007-07-08 18:51 ` Stefan Monnier
  2007-07-08 18:54   ` Jay Belanger
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier @ 2007-07-08 18:51 UTC (permalink / raw)
  To: jay.p.belanger; +Cc: emacs-devel

> However, if I have a file b.el containing
>    (defvar vara 2)
>    (defvar varb (eval-when-compile (+ 1 vara)))
> and then byte compile it (in a new Emacs session), I get an error
>     Symbol's value as variable is void: vara

Of course: there is no `vara' in the running Emacs: it's only present in the
b.el file which is being compiled but hasn't been loaded yet.

`require' is handled specially by the byte-compiler in that it doesn't just
place a call to `require' in the .elc file but it also loads the file during
byte-compilation.  `defvar' isn't special in this way.  Maybe we could make
`defconst' special in this way as well, but it hasn't seemed particularly
important until now.  You can just do

    (eval-and-compile (defvar vara 2))
    (defvar varb (eval-when-compile (+ 1 vara)))


-- Stefan

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

* Re: Odd behavior of `eval-when-compile'
  2007-07-08 18:51 ` Stefan Monnier
@ 2007-07-08 18:54   ` Jay Belanger
  0 siblings, 0 replies; 3+ messages in thread
From: Jay Belanger @ 2007-07-08 18:54 UTC (permalink / raw)
  To: emacs-devel; +Cc: jay.p.belanger


Stefan Monnier <monnier@iro.umontreal.ca> writes:
...
> Of course: there is no `vara' in the running Emacs: it's only present in the
> b.el file which is being compiled but hasn't been loaded yet.
...
>     (eval-and-compile (defvar vara 2))
>     (defvar varb (eval-when-compile (+ 1 vara)))

Thanks for the explanation; it makes sense now.

Jay

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

end of thread, other threads:[~2007-07-08 18:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-08  5:25 Odd behavior of `eval-when-compile' Jay Belanger
2007-07-08 18:51 ` Stefan Monnier
2007-07-08 18:54   ` Jay Belanger

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.