SBCL警告变量已定义但从未使用过

Oll*_*aus 7 sbcl common-lisp

我从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")))
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Kev*_*eid 14

您需要声明有意忽略该参数.

(defun worker-2 (context p)
  (declare (ignore context))
  (print p))
Run Code Online (Sandbox Code Playgroud)

ignore如果你也将发出警告信号使用变量.要在两种情况下禁止警告,您可以使用声明ignorable,但这只应在宏和其他此类情况下使用,在这种情况下无法确定变量是否将在声明时使用.

如果您还不熟悉declare,请注意它不是操作员,而只能出现在某些位置 ; 特别是,它必须位于defun身体中所有形式之前,尽管它可以位于文档字符串的上方或下方.