* How to define a macro correctly?
@ 2010-10-18 19:19 Johan Andersson
0 siblings, 0 replies; 2+ messages in thread
From: Johan Andersson @ 2010-10-18 19:19 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 739 bytes --]
Hey,
I want to create a macro that sets a variable value and then executes body.
I know how to solve it, but I want to know which way is the best (more
correct). I came up with these solutions:
a)
(defmacro mac (&rest body)
`(progn
(setq var t)
,@body))
b)
(defmacro mac (&rest body)
(setq var t)
`(progn ,@body))
c)
(defmacro mac (&rest body)
(cons 'progn (cons (list 'setq 'var t) body)))
I noticed that (using macroexpand) macro a and c expands to the same list. b
however sets the variable in the macro and then only return the list body.
What does that mean exactly, that I set the variable in the macro and do not
return it as a list?
What way is the best? Or is there some other way that is better?
Thanks!
[-- Attachment #2: Type: text/html, Size: 1045 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: How to define a macro correctly?
[not found] <mailman.8.1287429628.15066.help-gnu-emacs@gnu.org>
@ 2010-10-19 1:39 ` Barry Margolin
0 siblings, 0 replies; 2+ messages in thread
From: Barry Margolin @ 2010-10-19 1:39 UTC (permalink / raw)
To: help-gnu-emacs
In article <mailman.8.1287429628.15066.help-gnu-emacs@gnu.org>,
Johan Andersson <johan.rejeep@gmail.com> wrote:
> Hey,
>
> I want to create a macro that sets a variable value and then executes body.
> I know how to solve it, but I want to know which way is the best (more
> correct). I came up with these solutions:
>
> a)
> (defmacro mac (&rest body)
> `(progn
> (setq var t)
> ,@body))
>
> b)
> (defmacro mac (&rest body)
> (setq var t)
> `(progn ,@body))
>
> c)
> (defmacro mac (&rest body)
> (cons 'progn (cons (list 'setq 'var t) body)))
>
> I noticed that (using macroexpand) macro a and c expands to the same list. b
> however sets the variable in the macro and then only return the list body.
> What does that mean exactly, that I set the variable in the macro and do not
> return it as a list?
>
> What way is the best? Or is there some other way that is better?
>
> Thanks!
a and c are equivalent. The backquote expression in a is just shorthand
for the cons stuff you wrote in c (or something equivalent to it).
b is wrong. The setq happens once at compile time, not execution time.
You'll notice the difference when you byte-compile a file that uses the
macro.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2010-10-19 1:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-18 19:19 How to define a macro correctly? Johan Andersson
[not found] <mailman.8.1287429628.15066.help-gnu-emacs@gnu.org>
2010-10-19 1:39 ` Barry Margolin
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.