emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: eval-when-compile


From: PJ Weisberg
Subject: Re: eval-when-compile
Date: Fri, 27 Jul 2012 00:41:36 -0700

On Thu, Jul 26, 2012 at 11:26 PM, Pascal J. Bourguignon
<address@hidden> wrote:
> Achim Gratz <address@hidden> writes:

>> (eval-when-compile
>>   (if t
>>       (defvar unquoted-t "true")
>>     (defvar unquoted-nil "false")))

Try evaluating that block.  It has side effects, of course, but the result is:

'unquoted-t

That's just a symbol, sitting there all alone and not doing anything.
There are no instructions to execute, so the byte compiler doesn't
have anything to output.

>> (eval-when-compile
>>   (if nil
>>       '(defvar quoted-t "true")
>>     '(defvar quoted-nil "false")))

That evaluates to:

'(defvar quoted-nil "false")

That's a list.  Just a list, not a function call.  Once again, nothing
for the byte compiler to do.

eval-when-compile isn't a macro; it results in a value, not code.

>> (eval-when-compile
>>   (defmacro ewc-macro (&rest body)
>>     (if nil
>>       '(defvar macro-t "true")
>>       '(defvar macro-nil "false"))))
>> (ewc-macro)

*That's* a macro.  eval-when-compile still outputs a value, but the
macro call outputs code.  Here, the .elc file will be the equivalent
of:

'ewc-macro
(defvar macro-nil "false")

Line 1 is a no-op, but line 2 is just what you want.

> Here, you're defining a macro at compilation time.  When compiling this
> file, the macro is known and (ewc-macro) expands to (defvar macro-nil
> "false") so that's compiled into the elc file, and that's the only thing
> that's loaded.
>
> If you try to load the .el file directly, then the macro is not defined,
> and you're trying to call a function named (ewc-macro) which will fail.

That's incorrect.  Things inside the eval-when-compile get evaluated
when the .el file is loaded, but not when the .elc file is loaded.
It's *not* the equivalent of Common Lisp's (eval-when (compile)
body...)

-PJ

Gehm's Corollary to Clark's Law: Any technology distinguishable from
magic is insufficiently advanced.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]