Eval during emacs lisp macro expansion

How can I fix a simple macro fooin (elisp) Eval during extension ?

None of the following work:

(defmacro foo1 (a)
  `(setq (eval ,a) t))

(defmacro foo2 (a)
  `(setq ,(eval a) t))

(defmacro foo3 (a)
  `(setq ,a t))

I really don't understand what (elisp) Eval says during extension . I think if I succeeded, I could fix the macro.

Update: huaiyuan solution works:

(defmacro foo7 (a)
  `(set ,a t))

(setq x 'b 
      a 'c)

(foo7 x)
(assert (eq b t))
(assert (eq x 'b))

(foo7 a)
(assert (eq a 'c))
(assert (eq c t))

(macroexpand '(foo7 x)) ; ==> (set x t)
(macroexpand '(foo7 a)) ; ==> (set a t)
+3
source share
3 answers

Try

(defmacro foo7 (a)
  `(set, at))

The semantics of elisp are often random to implement. For an example of well-designed, well-defined macro systems, I recommend Common Lisp .

+2
source

""?

, , , , , . , , , , .

 (defmacro foo (aVeryLongAndImprobablyConflictingName)
   (list 'setq (eval aVeryLongAndImprobablyConflictingName) t))
0

A “correct” correction is to not require an evaluation of the parameters provided by the user in the macro distribution function.

(defmacro foo4 (a) `(setq, at))

Although this does NOT do the same as foo1, foo2 or foo3. What is the problem you are trying to solve?

0
source

Source: https://habr.com/ru/post/1722439/


All Articles