我从sbcl编译器收到一个警告,一个变量已被定义但未被使用。编译器是对的。我想摆脱警告,但不知道该怎么做。这是一个例子:
(defun worker-1 (context p)
;; check context (make use of context argument)
(if context
(print p)))
(defun worker-2 (context p)
;; don't care about context
;; will throw a warning about unused argument
(print p))
;;
;; calls a given worker with context and p
;; doesn't know which arguments will be used by the
;; implementation of the called worker
(defun do-cmd (workerFn context p)
(funcall workerFn context p))
(defun main ()
(let ((context ()))
(do-cmd #'worker-1 context "A")
(do-cmd #'worker-2 context "A")))
do-cmd-function期望实现特定接口f(context p)的worker-functions。
sbcl编译器抛出以下警告:
in: DEFUN WORKER-2
; (DEFUN WORKER-2 (CONTEXT P) (PRINT P))
;
; caught STYLE-WARNING:
; The variable CONTEXT is defined but never used.
;
; compilation unit finished
; caught 1 STYLE-WARNING condition
你需要声明参数是故意的ignored。
(defun worker-2 (context p)
(declare (ignore context))
(print p))
如果你使用变量,ignore
也会发出警告。要在两种情况下抑制警告,可以使用声明ignorable
,但这只应在宏和其他此类情况下使用,在这种情况下无法确定变量是否将在其声明点使用。
如果您还不熟悉declare
,请注意它不是运算符,而只能出现在certain locations中;特别是,它必须位于defun
体内的所有形式之前,尽管它可以在文档字符串的上方或下方。