all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: help-gnu-emacs@gnu.org
Subject: Re: Simple macro question
Date: Sun, 02 May 2021 21:46:30 -0400	[thread overview]
Message-ID: <jwv35v4y4re.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: 20210503000712.l2wh2liwhg3jfjxy@Ergus

> (defvar test-commands-list '(A B C)
>   "List of replaced functions.")
> (defmacro test-def ()
>   "Doc def."
>   `(eval-and-compile ,@(mapcar (lambda (com) nil) test-commands-list)))
> (test-def)

[...]

> Symbol’s value as variable is void: test-commands-list

macro-expansion takes place during compilation.  During compilation, the
code is mostly translated, not evaluated, but macros are expanded.
So your `(test-def)` macro call is expanded which evaluates the code
inside `test-def` whereas the `(defvar ...)` code is not evaluated (it's
only translated into byte-code) and hence the var is not defined: it
will only be defined much later when the resulting byte-code is run.

One way to work around it is to place the `defvar` within an
`eval-and-compile`.

> (defmacro test-def (commands-list)
> ...
>
> (test-def test-commands-list)
>
> In this second case I get: wrong arguments sequencep test-commands-list
> Is this intended?

Yes: the argument to macros are the actual sexp written in the macro
call, not the result of their evaluation.  So your argument
`commands-list` will hold the symbol `test-commands-list` rather than
the list you were hoping to get.  You could use `symbol-value` to get
the content of that symbol as a variable, but then you'd be back to the
previous problem because the `defvar` has not been evaluated yet.


        Stefan




  parent reply	other threads:[~2021-05-03  1:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210503000712.l2wh2liwhg3jfjxy.ref@Ergus>
2021-05-03  0:07 ` Simple macro question Ergus
2021-05-03  1:29   ` Ergus
2021-05-03  1:46   ` Stefan Monnier [this message]
2021-05-03 11:32     ` Ergus
2021-05-04 19:54       ` Stefan Monnier via Users list for the GNU Emacs text editor

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=jwv35v4y4re.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --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.