Proclaim, declaim, declare

时光毁灭记忆、已成空白 提交于 2020-06-24 03:44:29

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14813801/proclaim-declaim-declare

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!