99 lines
11 KiB
HTML
99 lines
11 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 LOAD-TIME-VALUE</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="s_eval_w.htm">
|
|
<LINK REL=UP HREF="c_evalua.htm">
|
|
<LINK REL=NEXT HREF="s_quote.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="s_eval_w.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_quote.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="load-time-value"><I>Special Operator</I> <B>LOAD-TIME-VALUE</B></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>load-time-value</B> <I>form <TT>&optional</TT> read-only-p</I> => <I>object</I><P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; evaluated as described below. <P>
|
|
<I>read-only-p</I>---a <A REL=DEFINITION HREF="26_glo_b.htm#boolean"><I>boolean</I></A>; not evaluated. <P>
|
|
<I>object</I>---the <A REL=DEFINITION HREF="26_glo_p.htm#primary_value"><I>primary value</I></A> resulting from evaluating <I>form</I>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#load-time-value"><B>load-time-value</B></A> provides a mechanism for delaying evaluation of <I>form</I> until the expression is in the run-time environment; see <A REL=CHILD HREF="03_b.htm">Section 3.2 (Compilation)</A>. <P>
|
|
<I>Read-only-p</I> designates whether the result can be considered a <A REL=DEFINITION HREF="26_glo_c.htm#constant_object"><I>constant object</I></A>. If <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A>, the result is a read-only quantity that can, if appropriate to the <A REL=DEFINITION HREF="26_glo_i.htm#implementation"><I>implementation</I></A>, be copied into read-only space and/or <I>coalesced</I> with <A REL=DEFINITION HREF="26_glo_s.htm#similar"><I>similar</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#constant_object"><I>constant objects</I></A> from other <A REL=DEFINITION HREF="26_glo_p.htm#program"><I>programs</I></A>. If <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> (the default), the result must be neither copied nor coalesced; it must be considered to be potentially modifiable data. <P>
|
|
If a <A REL=DEFINITION HREF="#load-time-value"><B>load-time-value</B></A> expression is processed by <A REL=DEFINITION HREF="f_cmp_fi.htm#compile-file"><B>compile-file</B></A>, the compiler performs its normal semantic processing (such as macro expansion and translation into machine code) on <I>form</I>, but arranges for the execution of <I>form</I> to occur at load time in a <A REL=DEFINITION HREF="26_glo_n.htm#null_lexical_environment"><I>null lexical environment</I></A>, with the result of this <A REL=DEFINITION HREF="26_glo_e.htm#evaluation"><I>evaluation</I></A> then being treated as a <A REL=DEFINITION HREF="26_glo_l.htm#literal"><I>literal</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> at run time. It is guaranteed that the evaluation of <I>form</I> will take place only once when the <A REL=DEFINITION HREF="26_glo_f.htm#file"><I>file</I></A> is <I>loaded</I>, but the order of evaluation with respect to the evaluation of <A REL=DEFINITION HREF="26_glo_t.htm#top_level_form"><I>top level forms</I></A> in the file is <A REL=DEFINITION HREF="26_glo_i.htm#implementation-dependent"><I>implementation-dependent</I></A>. <P>
|
|
If a <A REL=DEFINITION HREF="#load-time-value"><B>load-time-value</B></A> expression appears within a function compiled with <A REL=DEFINITION HREF="f_cmp.htm#compile"><B>compile</B></A>, the <I>form</I> is evaluated at compile time in a <A REL=DEFINITION HREF="26_glo_n.htm#null_lexical_environment"><I>null lexical environment</I></A>. The result of this compile-time evaluation is treated as a <A REL=DEFINITION HREF="26_glo_l.htm#literal"><I>literal</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> in the compiled code. <P>
|
|
If a <A REL=DEFINITION HREF="#load-time-value"><B>load-time-value</B></A> expression is processed by <A REL=DEFINITION HREF="f_eval.htm#eval"><B>eval</B></A>, <I>form</I> is evaluated in a <A REL=DEFINITION HREF="26_glo_n.htm#null_lexical_environment"><I>null lexical environment</I></A>, and one value is returned. Implementations that implicitly compile (or partially compile) expressions processed by <A REL=DEFINITION HREF="f_eval.htm#eval"><B>eval</B></A> might evaluate <I>form</I> only once, at the time this compilation is performed. <P>
|
|
If the <A REL=DEFINITION HREF="26_glo_s.htm#same"><I>same</I></A> <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> <TT>(load-time-value </TT><I>form</I><TT>)</TT> is evaluated or compiled more than once, it is <A REL=DEFINITION HREF="26_glo_i.htm#implementation-dependent"><I>implementation-dependent</I></A> whether <I>form</I> is evaluated only once or is evaluated more than once. This can happen both when an expression being evaluated or compiled shares substructure, and when the <A REL=DEFINITION HREF="26_glo_s.htm#same"><I>same</I></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> is processed by <A REL=DEFINITION HREF="f_eval.htm#eval"><B>eval</B></A> or <A REL=DEFINITION HREF="f_cmp.htm#compile"><B>compile</B></A> multiple times. Since a <A REL=DEFINITION HREF="#load-time-value"><B>load-time-value</B></A> expression can be referenced in more than one place and can be evaluated multiple times by <A REL=DEFINITION HREF="f_eval.htm#eval"><B>eval</B></A>, it is <A REL=DEFINITION HREF="26_glo_i.htm#implementation-dependent"><I>implementation-dependent</I></A> whether each execution returns a fresh <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> or returns the same <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> as some other execution. Users must use caution when destructively modifying the resulting <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P>
|
|
If two lists <TT>(load-time-value </TT><I>form</I><TT>)</TT> that are the <A REL=DEFINITION HREF="26_glo_s.htm#same"><I>same</I></A> under <A REL=DEFINITION HREF="f_equal.htm#equal"><B>equal</B></A> but are not <A REL=DEFINITION HREF="26_glo_i.htm#identical"><I>identical</I></A> are evaluated or compiled, their values always come from distinct evaluations of <I>form</I>. Their <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> may not be coalesced unless <I>read-only-p</I> is <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A>. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
;;; The function INCR1 always returns the same value, even in different images.
|
|
;;; The function INCR2 always returns the same value in a given image,
|
|
;;; but the value it returns might vary from image to image.
|
|
(defun incr1 (x) (+ x #.(random 17)))
|
|
(defun incr2 (x) (+ x (load-time-value (random 17))))
|
|
|
|
;;; The function FOO1-REF references the nth element of the first of
|
|
;;; the *FOO-ARRAYS* that is available at load time. It is permissible for
|
|
;;; that array to be modified (e.g., by SET-FOO1-REF); FOO1-REF will see the
|
|
;;; updated values.
|
|
(defvar *foo-arrays* (list (make-array 7) (make-array 8)))
|
|
(defun foo1-ref (n) (aref (load-time-value (first *my-arrays*) nil) n))
|
|
(defun set-foo1-ref (n val)
|
|
(setf (aref (load-time-value (first *my-arrays*) nil) n) val))
|
|
|
|
;;; The function BAR1-REF references the nth element of the first of
|
|
;;; the *BAR-ARRAYS* that is available at load time. The programmer has
|
|
;;; promised that the array will be treated as read-only, so the system
|
|
;;; can copy or coalesce the array.
|
|
(defvar *bar-arrays* (list (make-array 7) (make-array 8)))
|
|
(defun bar1-ref (n) (aref (load-time-value (first *my-arrays*) t) n))
|
|
|
|
;;; This use of LOAD-TIME-VALUE permits the indicated vector to be coalesced
|
|
;;; even though NIL was specified, because the object was already read-only
|
|
;;; when it was written as a literal vector rather than created by a constructor.
|
|
;;; User programs must treat the vector v as read-only.
|
|
(defun baz-ref (n)
|
|
(let ((v (load-time-value #(A B C) nil)))
|
|
(values (svref v n) v)))
|
|
|
|
;;; This use of LOAD-TIME-VALUE permits the indicated vector to be coalesced
|
|
;;; even though NIL was specified in the outer situation because T was specified
|
|
;;; in the inner situation. User programs must treat the vector v as read-only.
|
|
(defun baz-ref (n)
|
|
(let ((v (load-time-value (load-time-value (vector 1 2 3) t) nil)))
|
|
(values (svref v n) v)))
|
|
</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=DEFINITION HREF="f_cmp.htm#compile"><B>compile</B></A>, <A REL=DEFINITION HREF="f_eval.htm#eval"><B>eval</B></A>, <A REL=CHILD HREF="03_bbb.htm">Section 3.2.2.2 (Minimal Compilation)</A>, <A REL=CHILD HREF="03_b.htm">Section 3.2 (Compilation)</A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#load-time-value"><B>load-time-value</B></A> must appear outside of quoted structure in a ``for <A REL=DEFINITION HREF="26_glo_e.htm#evaluation"><I>evaluation</I></A>'' position. In situations which would appear to call for use of <A REL=DEFINITION HREF="#load-time-value"><B>load-time-value</B></A> within a quoted structure, the <A REL=DEFINITION HREF="26_glo_b.htm#backquote"><I>backquote</I></A> <A REL=DEFINITION HREF="26_glo_r.htm#reader_macro"><I>reader macro</I></A> is probably called for; see <A REL=CHILD HREF="02_df.htm">Section 2.4.6 (Backquote)</A>. <P>
|
|
Specifying <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> for <I>read-only-p</I> is not a way to force an object to become modifiable if it has already been made read-only. It is only a way to say that, for an object that is modifiable, this operation is not intended to make that object read-only. <P>
|
|
<P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss217.htm">LOAD-TIME-EVAL:R**3-NEW-SPECIAL-FORM</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>
|