[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Using the content of a dynamic variable in a macro
From: |
Michael Heerdegen |
Subject: |
Re: Using the content of a dynamic variable in a macro |
Date: |
Sun, 26 Feb 2023 12:47:57 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Damien Cassou <damien@cassou.me> writes:
> I'm wondering why the code below works but won't compile.
>
> foo.el:
> (defvar foo-var '((message "hello world")))
>
> (defmacro foo-macro ()
> `(progn
> ,@foo-var))
>
> (defun foo-fun ()
> (foo-macro))
> [...]
> foo.el:32:1: Error: Symbol’s value as variable is void: foo-var
>
> Why isn't the compiler aware of the foo-var variable?
The compiler is aware of it, but it doesn't eval the definition. It is
a feature that the compiler normally does not load any part of the
program when compiling, including variable definitions. Note that in
the general case, evaluating a defvar form might require to load more
parts of a file, or even the complete file.
If you do need to eval something when compiling, use `eval-when-compile'
or `eval-and-compile' respectively.
Michael.