我寫了一個函數,它可以接受任何類型的任意數量的參數,並且它可以打印參數的名稱和值。該功能按預期工作。但我不喜歡函數調用要求我傳遞如此的價值報價(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.
什麼問題?
這就是爲什麼生成[2倍]的真正原因。 – tom