【发布时间】:2016-03-23 04:45:40
【问题描述】:
我编写了一个函数,它可以接受任何种类、任意数量的参数,以便它可以打印参数的名称和值。该功能按预期工作。但我不喜欢函数调用要求我传递像(my-message 'emacs-version 'emacs-copyright) 这样的值引用。我想简化为(my-message emacs-version emacs-copyright)。所以我用宏来重写函数。
(defmacro my-message (&rest args)
(if args
(progn
(message "This is the start of debug message.\n")
(dolist (arg args)
(cond
((stringp arg)
(message arg))
((numberp arg)
(message (number-to-string arg)))
((boundp arg)
(pp arg)
(message "")
(pp (symbol-value arg)))
((not (boundp arg))
(pp arg)
(message "Undefined")))
(message "\n"))
(message "This is the end of debug message."))
(message "This is a debug message ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")))
但是,有些消息被打印了两次。
(my-message emacs-version emacs-copyright 12345 "HelloWorld" foobar)
This is the start of debug message.
emacs-version
"24.5.1"
[2 times]
emacs-copyright
"Copyright (C) 2015 Free Software Foundation, Inc."
[2 times]
12345
[2 times]
HelloWorld
[2 times]
foobar
Undefined
[2 times]
This is the end of debug message.
有什么问题?
【问题讨论】: