help-gnu-emacs
[Top][All Lists]
Advanced

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

RE: [External] : Re: Passing result of macroexpand-all as argument to a


From: Drew Adams
Subject: RE: [External] : Re: Passing result of macroexpand-all as argument to a function
Date: Wed, 9 Aug 2023 04:14:34 +0000

> I want to pass other variables to my function, example a a buffer name
> like so
> (myfunc ` ,(macroexpand-all something arg) bfname)

No problem. The backquote+comma apply only to the
expression that immediately follows.

The call to myfunc evaluates all of its arguments.

The backquote+comma evaluates
(macroexpand-all something arg) and quotes the result.
That quoted value gets passed as the argument.

That arg gets evaluated normally, yielding the result
of evaluating (macroexpand-all something arg).

The second arg, variable bfname, is evaluated as an
arg, as usual.

E.g., if (macroexpand-all something arg) expands to
the variable foobar, then 'foobar is passed to
myfunc, along with bfname.  So the symbol foobar is
passed as the first arg, and the value of variable
bfname is passed as the second arg.

If (macroexpand-all something arg) instead expands
to (foobar toto) then '(foobar toto) is passed to
myfunc.  Since the arg is evaluated for the myfunc
function call, the list (foobar toto) is passed:
the result of evaluating '(foobar toto), aka
(quote (foobar toto)).

> What I am worried about is this.  Would myfunc get confused when the code
> of macroexpand-all ends and when bfname starts ?

No.  The result of evaluating (macroexpand-all...)
is a Lisp object.  That's quoted and passed to
myfunc.  The result of evaluating that quoted
result of macroexpansion is that result (unquoted).
The result of evaluating variable bfname is a
another Lisp object.

> If I understand correctly, just calling would get the result from
> (macroexpand-all something arg) but then evaluate it because the macro
> code changes in context when used as an argument to a function.

Dunno what you mean by that.

> (myfunc (macroexpand-all something arg) bfname)

That would pass the result of macroexpansion as
the first arg to myfunc.  Evaluating the myfunc
function call would _evaluate_ that arg.  You
apparently don't want that, so you quote it.
To do that, use `,(macroexpand-all something arg).

I said, above, "The backquote+comma apply only
to the expression that immediately follows."
A better way to look at it is that the ` really
envelopes the sexp that follows it.  Really, what
happens is that the `... is shorthand for
(backquote ...).

The symbol whose name is just a backquote char
(`) is an alias for the macro named "backquote":

(defalias '\` (symbol-function 'backquote))

Here you see that the sexp that follows the ` is
really just an arg passed to macro backquote:

(macroexpand (backquote ',(setq foo 42))) ; '42
(macroexpand (backquote '(setq foo 42)))  ; '(setq foo 42)

(macroexpand `',(setq foo 42)) ; '42
(macroexpand `'(setq foo 42))  ; '(setq foo 42)

C-h f backquote, or `C-h f `, to learn more.

> I just say things how they are.  If I do not understand
> and the manual is of no help (to me of course), no
> amount of abuse from anyone will convince my understanding.

No one is abusing you.  Soon maybe you will help
others with Lisp.  Everyone can learn; everyone
can teach.  Everyone needs help.  Everyone can
appreciate help, and show it by helping others.

Someone who only asks for help but doesn't help
will sooner or later lack others willing to help.
Changing names only works locally & temporarily.

reply via email to

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