9

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?

PierreBdR
  • 42,120
  • 10
  • 46
  • 62
  • 1
    possible duplicate of [How do I ask the Lisp compiler to ignore a (label-variety) function?](http://stackoverflow.com/questions/9457103/how-do-i-ask-the-lisp-compiler-to-ignore-a-label-variety-function) – Joshua Taylor Mar 06 '14 at 18:05
  • 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. – PierreBdR Mar 06 '14 at 18:19
  • 1
    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. – Joshua Taylor Mar 06 '14 at 18:25
  • @PierreBdR I'm just curious... You're doing something related to lambda calculus, right? Cause it looks exactly like `TRUE` in standard lambda calculus. – Wojciech Gac Mar 06 '14 at 21:31
  • @WojciechGac Yes I am. I am playing with ideas from 'Programming with Nothing' – PierreBdR Mar 07 '14 at 07:04

1 Answers1

21

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.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • 1
    Thanks. I had seen this ignore thing, but not the examples and how to use it properly. – PierreBdR Mar 06 '14 at 17:37
  • 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 round – Patrick Mar 07 '14 at 11:47
  • 7
    @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`. – Rainer Joswig Mar 07 '14 at 12:12