“返回的值未被使用”字节编译宏时发出警告 [英] "value returned is unused" warning when byte-compiling a macro

查看:159
本文介绍了“返回的值未被使用”字节编译宏时发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (defmacro foomacro( 
`(defun foo(& optional arg)
(interactive,(concat shiftcodep))
(messagearg is%iarg))
'(defun bar(& optional arg)
(interactive,(concat shiftcodeNenter a number:))
(messagearg is%iarg)))
;;为Emacs提供向后兼容22
(if(fboundp'handle-shift-selection)
(foomacro^)
(foomacro))

这是我得到的警告:

 $ emacs -Q --batch --eval'(byte-compile-filefoo.el)'

在foomacro中:
foo.el :1:21:警告:从(concat shiftcodep)返回的值未使用

如果我摆脱 bar ,警告消失:

 <$ 
(交互式,(concat shiftcodep))
(消息arg是%i arg)))
;;为Emacs提供向后兼容22
(if(fboundp'handle-shift-selection)
(foomacro^)
(foomacro))

我使用的是GNU Emacs 24.2.1。

解决方案

这是因为你忘了将宏体包裹在一个progn中:

 (defmacro foomacro(shiftcode)
`(progn
(defun foo(& optional arg)
(interactive,(concat shiftcodep))
(messagearg is%iarg))
(defun bar(& optional arg)
(interactive,(concat shiftcodeNenter a number:))
(messagearg is%iarg))))

想想宏如何工作。当您调用(foomacro...)时,lisp引擎会识别 foomacro 是一个宏, >扩展它,即调用它提供的参数。宏的返回值与预期的 第二个 defun 形式;而第一个 defun 表单已被弃用。然后,lisp引擎评估返回值(这是第二个 defun 形式)。因此,在 progn 无版本中,只定义 bar ,而不是 foo



为了理解这个过程,你需要意识到宏只是代码转换工具;他们真的不做任何事情。因此,编译器(或解释器)只能看到它们的返回值。


Why does byte-compiling the the following produce a warning?

(defmacro foomacro (shiftcode)
  `(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)))
;; provide backward compatibility for Emacs 22
(if (fboundp 'handle-shift-selection)
    (foomacro "^")
  (foomacro ""))

This is the warning I get:

$ emacs -Q --batch --eval '(byte-compile-file "foo.el")'

In foomacro:
foo.el:1:21:Warning: value returned from (concat shiftcode "p") is unused

If I get rid of bar, the warning goes away:

(defmacro foomacro (shiftcode)
  `(defun foo (&optional arg)
     (interactive ,(concat shiftcode "p"))
     (message "arg is %i" arg)))
;; provide backward compatibility for Emacs 22
(if (fboundp 'handle-shift-selection)
    (foomacro "^")
  (foomacro ""))

I'm using GNU Emacs 24.2.1.

解决方案

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).

这篇关于“返回的值未被使用”字节编译宏时发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆