“value returned is unused” warning when byte-compiling a macro

本小妞迷上赌 提交于 2019-12-06 13:54:36

That's because you forgot to wrap the macro body in a progn:

(defmacro foomacro (shiftcode)
  `(progn
     (defun foo (&optional arg)
       (interactive ,(concat shiftcode "p"))
       (message "arg is %i" arg))
     (defun bar (&optional arg)
       (interactive ,(concat shiftcode "Nenter a number: "))
       (message "arg is %i" arg))))

Think about how macros work. When you call (foomacro "..."), the lisp engine recognizes that foomacro is a macro and expands it, i.e., calls it on the argument(s) supplied. the return value of the macro is, as expected, the second defun form; while the first defun form is discarded. Then the lisp engine evaluates the return value (which is the second defun form). Thus in your progn-less version only bar is defined, not foo.

To understand the process, you need to realise that macros are merely "code transformation" tools; they don't really do anything. Thus only their return values are seen by the compiler (or the interpreter).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!