Proclaim, declaim, declare
Asked Answered
S

1

27

Can you please explain the differences between the three symbols proclaim, declaim and declare?

Scintillometer answered 11/2, 2013 at 14:14 Comment(0)
B
38

They are symbols, not keywords.

  1. proclaim names a function for making global declarations. You should use declaim instead whenever possible.

  2. declaim names a macro for making global declarations (like proclaim) which are also effective at compile-time.

  3. 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.

Boutin answered 11/2, 2013 at 16:12 Comment(6)
Thank you sds! Could you clarify the difference between proclaim and declaim? I have changed the word "keyword" in my question, it was confusing. I was trying to explicitely hide the kind of symbol, but the word symbol was just as fine.Scintillometer
The difference between them is just that: one is a function and, ordinarily, has no effect at compilation time, while the other is a macro which affects the compilation environment. You really need to read the spec for details.Boutin
Alright, I get it now, thanks. Honestly the Hyperspec is a bit hard to understand, but I will try to refer to it in the future.Scintillometer
The spec is the reference; it is best to start with a textbook, like Graham's ANSI CL or Seibel's Practical CL.Boutin
sds: you have written some great answers to other questions in the past, and while your summary of the nature of proclaim, declaim and declare are very true, the rest of the answer boils down to read a book or read the hyperspec. I was pretty excited to see the answer to this question as, while I use declaim, I don't have a good intuition of its behaviour and I find the hyperspec's info on the subject is a little dense. Any chance you could expand this answer to include the behaviour of the functions & macros in question? ThanksCommercial
@Baggers, thanks this is exactly the reason why I asked this question, and thanks to sds for enriching it.Scintillometer

© 2022 - 2024 — McMap. All rights reserved.