Suppress warning for function arguments not used in LISP
Asked Answered
K

1

10

In lisp, I need to define a set of functions, all with the same number of arguments. However, the functions may or may not use all the arguments, leading to a spur of warning messages. For example:

(defun true (X Y) X)
[...]
; caught STYLE-WARNING:
;   The variable Y is defined but never used.

Is there a way to warn the compiler that is was intended?

Kiesha answered 6/3, 2014 at 17:18 Comment(5)
possible duplicate of How do I ask the Lisp compiler to ignore a (label-variety) function?Discernible
Although it turns out the answer is the same, this is a specificity of LISP, and not because the questions are the same. In other terms, I think both questions are needed as different people will find them.Kiesha
I'm not sure what you mean by "specificity of LISP"; this question is about Common Lisp, and so is the possible duplicate. Also, closing the question as a duplicate doesn't mean deleting the question. Duplicate questions can in fact be useful because they serve as signposts to the original question. I think that this is a good question. If there's an answer at the other question, then closing as a duplicate says "look over there, there's an answer over there". Also, it takes five votes to close a question, so at least a few other people would have to agree before it can be closed.Discernible
@Kiesha I'm just curious... You're doing something related to lambda calculus, right? Cause it looks exactly like TRUE in standard lambda calculus.Pierrette
@WojciechGac Yes I am. I am playing with ideas from 'Programming with Nothing'Kiesha
S
23

See the Common Lisp Hyperspec: Declaration IGNORE, IGNORABLE

A variable is not used. Ignore it.

(defun true (x y)
  (declare (ignore y))
  x)

Above tells the compiler that y is not going to be used.

The compiler will complain if it is used. It will not complain if it is not used.

A variable might not be used. Don't care.

(defun true (x y)
  (declare (ignorable y))
  x)

Above tells the compiler that y might not be used.

The compiler will not complain if it is used and also not if it is not used.

Sams answered 6/3, 2014 at 17:28 Comment(3)
Thanks. I had seen this ignore thing, but not the examples and how to use it properly.Kiesha
Correct, but in SBCL "ignore y" does not include the option to "ignore the ignore y". That often becomes an issue with macros, where you receive complaints sometimes the one and sometimes the other way roundAggie
@Patrick: IGNORE should not be ignored. The compiler should complain, if the variable is actually used. If you are not sure if a variable will be used or not, and you don't care, then use IGNORABLE.Sams

© 2022 - 2024 — McMap. All rights reserved.