157 lines
12 KiB
HTML
157 lines
12 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Special Operator EVAL-WHEN</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="f_eval.htm">
|
|
<LINK REL=UP HREF="c_evalua.htm">
|
|
<LINK REL=NEXT HREF="s_ld_tim.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="f_eval.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_evalua.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="s_ld_tim.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="eval-when"><I>Special Operator</I> <B>EVAL-WHEN</B></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>eval-when</B> <I>(<I>situation</I><B>*</B>) <I>form</I><B>*</B></I> => <I><I>result</I><B>*</B></I><P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>situation</I>---One of the <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbols</I></A> <TT>:compile-toplevel</TT>, <TT>:load-toplevel</TT>, <TT>:execute</TT>, <A REL=DEFINITION HREF="f_cmp.htm#compile"><B>compile</B></A>, <A REL=DEFINITION HREF="f_load.htm#load"><B>load</B></A>, or <A REL=DEFINITION HREF="f_eval.htm#eval"><B>eval</B></A>. <P>
|
|
The use of <A REL=DEFINITION HREF="f_eval.htm#eval"><B>eval</B></A>, <A REL=DEFINITION HREF="f_cmp.htm#compile"><B>compile</B></A>, and <A REL=DEFINITION HREF="f_load.htm#load"><B>load</B></A> is deprecated. <P>
|
|
<I>forms</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>. <P>
|
|
<I>results</I>---the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> of the <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A> if they are executed, or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> if they are not. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
The body of an <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> form is processed as an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>, but only in the <I>situations</I> listed. <P>
|
|
The use of the <I>situations</I> <TT>:compile-toplevel</TT> (or <TT>compile</TT>) and <TT>:load-toplevel</TT> (or <TT>load</TT>) controls whether and when <A REL=DEFINITION HREF="26_glo_e.htm#evaluation"><I>evaluation</I></A> occurs when <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> appears as a <A REL=DEFINITION HREF="26_glo_t.htm#top_level_form"><I>top level form</I></A> in code processed by <A REL=DEFINITION HREF="f_cmp_fi.htm#compile-file"><B>compile-file</B></A>. See <A REL=CHILD HREF="03_bc.htm">Section 3.2.3 (File Compilation)</A>. <P>
|
|
The use of the <I>situation</I> <TT>:execute</TT> (or <TT>eval</TT>) controls whether evaluation occurs for other <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A>; that is, those that are not <A REL=DEFINITION HREF="26_glo_t.htm#top_level_form"><I>top level forms</I></A>, or those in code processed by <A REL=DEFINITION HREF="f_eval.htm#eval"><B>eval</B></A> or <B>compile</B>. If the <TT>:execute</TT> situation is specified in such a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>, then the body <I>forms</I> are processed as an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>; otherwise, the <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> normally appears as a <A REL=DEFINITION HREF="26_glo_t.htm#top_level_form"><I>top level form</I></A>, but it is meaningful for it to appear as a <A REL=DEFINITION HREF="26_glo_n.htm#non-top-level_form"><I>non-top-level form</I></A>. However, the compile-time side effects described in <A REL=CHILD HREF="03_b.htm">Section 3.2 (Compilation)</A> only take place when <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> appears as a <A REL=DEFINITION HREF="26_glo_t.htm#top_level_form"><I>top level form</I></A>. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
One example of the use of <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> is that for the compiler to be able to read a file properly when it uses user-defined <A REL=DEFINITION HREF="26_glo_r.htm#reader_macro"><I>reader macros</I></A>, it is necessary to write <P>
|
|
<PRE>
|
|
(eval-when (:compile-toplevel :load-toplevel :execute)
|
|
(set-macro-character #\$ #'(lambda (stream char)
|
|
(declare (ignore char))
|
|
(list 'dollar (read stream))))) => T
|
|
</PRE>
|
|
</TT> This causes the call to <A REL=DEFINITION HREF="f_set_ma.htm#set-macro-character"><B>set-macro-character</B></A> to be executed in the compiler's execution environment, thereby modifying its reader syntax table. <P>
|
|
<PRE>
|
|
;;; The EVAL-WHEN in this case is not at toplevel, so only the :EXECUTE
|
|
;;; keyword is considered. At compile time, this has no effect.
|
|
;;; At load time (if the LET is at toplevel), or at execution time
|
|
;;; (if the LET is embedded in some other form which does not execute
|
|
;;; until later) this sets (SYMBOL-FUNCTION 'FOO1) to a function which
|
|
;;; returns 1.
|
|
(let ((x 1))
|
|
(eval-when (:execute :load-toplevel :compile-toplevel)
|
|
(setf (symbol-function 'foo1) #'(lambda () x))))
|
|
|
|
;;; If this expression occurs at the toplevel of a file to be compiled,
|
|
;;; it has BOTH a compile time AND a load-time effect of setting
|
|
;;; (SYMBOL-FUNCTION 'FOO2) to a function which returns 2.
|
|
(eval-when (:execute :load-toplevel :compile-toplevel)
|
|
(let ((x 2))
|
|
(eval-when (:execute :load-toplevel :compile-toplevel)
|
|
(setf (symbol-function 'foo2) #'(lambda () x)))))
|
|
|
|
;;; If this expression occurs at the toplevel of a file to be compiled,
|
|
;;; it has BOTH a compile time AND a load-time effect of setting the
|
|
;;; function cell of FOO3 to a function which returns 3.
|
|
(eval-when (:execute :load-toplevel :compile-toplevel)
|
|
(setf (symbol-function 'foo3) #'(lambda () 3)))
|
|
|
|
;;; #4: This always does nothing. It simply returns NIL.
|
|
(eval-when (:compile-toplevel)
|
|
(eval-when (:compile-toplevel)
|
|
(print 'foo4)))
|
|
|
|
;;; If this form occurs at toplevel of a file to be compiled, FOO5 is
|
|
;;; printed at compile time. If this form occurs in a non-top-level
|
|
;;; position, nothing is printed at compile time. Regardless of context,
|
|
;;; nothing is ever printed at load time or execution time.
|
|
(eval-when (:compile-toplevel)
|
|
(eval-when (:execute)
|
|
(print 'foo5)))
|
|
|
|
;;; If this form occurs at toplevel of a file to be compiled, FOO6 is
|
|
;;; printed at compile time. If this form occurs in a non-top-level
|
|
;;; position, nothing is printed at compile time. Regardless of context,
|
|
;;; nothing is ever printed at load time or execution time.
|
|
(eval-when (:execute :load-toplevel)
|
|
(eval-when (:compile-toplevel)
|
|
(print 'foo6)))
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Affected By:</B> None.
|
|
<P>
|
|
<P><B>Exceptional Situations:</B> None.
|
|
<P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="f_cmp_fi.htm#compile-file"><B>compile-file</B></A>, <A REL=CHILD HREF="03_b.htm">Section 3.2 (Compilation)</A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
The following effects are logical consequences of the definition of <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A>: <P>
|
|
<P><DL><DT>* Execution of a single <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> expression executes the body code at most once. <P><DD>
|
|
<DT>* <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>Macros</I></A> intended for use in <A REL=DEFINITION HREF="26_glo_t.htm#top_level_form"><I>top level forms</I></A> should be written so that side-effects are done by the <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A> in the macro expansion. The macro-expander itself should not do the side-effects. <P><DD>
|
|
For example: <P>
|
|
Wrong: <P>
|
|
<PRE>
|
|
(defmacro foo ()
|
|
(really-foo)
|
|
`(really-foo))
|
|
</PRE>
|
|
</TT> <P>
|
|
Right: <P>
|
|
<PRE>
|
|
(defmacro foo ()
|
|
`(eval-when (:compile-toplevel :execute :load-toplevel) (really-foo)))
|
|
</PRE>
|
|
</TT> <P>
|
|
Adherence to this convention means that such <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macros</I></A> behave intuitively when appearing as <A REL=DEFINITION HREF="26_glo_n.htm#non-top-level_form"><I>non-top-level forms</I></A>. <P>
|
|
<DT>* Placing a variable binding around an <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> reliably captures the binding because the compile-time-too mode cannot occur (i.e., introducing a variable binding means that the <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> is not a <A REL=DEFINITION HREF="26_glo_t.htm#top_level_form"><I>top level form</I></A>). For example, <P><DD>
|
|
<PRE>
|
|
(let ((x 3))
|
|
(eval-when (:execute :load-toplevel :compile-toplevel) (print x)))
|
|
</PRE>
|
|
</TT> <P>
|
|
prints <TT>3</TT> at execution (i.e., load) time, and does not print anything at compile time. This is important so that expansions of <A REL=DEFINITION HREF="m_defun.htm#defun"><B>defun</B></A> and <A REL=DEFINITION HREF="m_defmac.htm#defmacro"><B>defmacro</B></A> can be done in terms of <A REL=DEFINITION HREF="#eval-when"><B>eval-when</B></A> and can correctly capture the <A REL=DEFINITION HREF="26_glo_l.htm#lexical_environment"><I>lexical environment</I></A>. <P>
|
|
<PRE>
|
|
(defun bar (x) (defun foo () (+ x 3)))
|
|
</PRE>
|
|
</TT> <P>
|
|
might expand into <P>
|
|
<PRE>
|
|
(defun bar (x)
|
|
(progn (eval-when (:compile-toplevel)
|
|
(compiler::notice-function-definition 'foo '(x)))
|
|
(eval-when (:execute :load-toplevel)
|
|
(setf (symbol-function 'foo) #'(lambda () (+ x 3))))))
|
|
</PRE>
|
|
</TT> <P>
|
|
which would be treated by the above rules the same as <P>
|
|
<PRE>
|
|
(defun bar (x)
|
|
(setf (symbol-function 'foo) #'(lambda () (+ x 3))))
|
|
</PRE>
|
|
</TT> <P>
|
|
when the definition of <TT>bar</TT> is not a <A REL=DEFINITION HREF="26_glo_t.htm#top_level_form"><I>top level form</I></A>. <P></DL><P>
|
|
<P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss104.htm">DEFINING-MACROS-NON-TOP-LEVEL:ALLOW</A><LI> <A REL=CHILD HREF="../Issues/iss148.htm">EVAL-WHEN-OBSOLETE-KEYWORDS:X3J13-MAR-1993</A><LI> <A REL=CHILD HREF="../Issues/iss147.htm">EVAL-WHEN-NON-TOP-LEVEL:GENERALIZE-EVAL-NEW-KEYWORDS</A><P></UL><HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|