Can you please explain the differences between the three symbols proclaim
, declaim
and declare
?
They are symbols, not keywords.
proclaim names a function for making global declarations. You should use declaim instead whenever possible.
declaim names a macro for making global declarations (like proclaim) which are also effective at compile-time.
declare is just a symbol (i.e., it does not name a function, macro, or special operator) for making local declarations in the beginning of some forms (you can view it as an element of syntax of those forms).
So, the first two affect the global environment and the last one is local.
declaim is preferable over proclaim because it has an immediate effect in the compilation environment:
Although the execution of a proclaim form has effects that might affect compilation, the compiler does not make any attempt to recognize and specially process proclaim forms. A proclamation such as the following, even if a top level form, does not have any effect until it is executed:
(proclaim '(special *x*))
If compile time side effects are desired, eval-when may be useful. For example:
(eval-when (:execute :compile-toplevel :load-toplevel) (proclaim '(special *x*)))
In most such cases, however, it is preferrable to use declaim for this purpose.
I.e., if your code is
(proclaim '(special *x*))
(defun foo () (print *x*))
the compiler will complain that foo
reads an unknown special variable *x*
, while
(declaim (special *x*))
(defun foo () (print *x*))
will cause no warnings.
PS. If you are wondering why CL even has proclaim: first, historically it was there before declaim, and, second, proclaim is simpler and more useful in macros.