![[LISPWORKS]](/image.pl?url=http%3a%2f%2fwww.lispworks.com%2fdocumentation%2fHyperSpec%2fBody%2f..%2fGraphics%2fLWSmall.gif&f=jpg&w=240)
![[Common Lisp HyperSpec (TM)]](/image.pl?url=http%3a%2f%2fwww.lispworks.com%2fdocumentation%2fHyperSpec%2fBody%2f..%2fGraphics%2fCLHS_Sm.gif&f=jpg&w=240)
Syntax:
load-time-valueform&optional read-only-p =>object
Arguments and Values:
form---aform; evaluated as described below.
read-only-p---aboolean; not evaluated.
object---theprimary value resulting from evaluatingform.
Description:
load-time-value provides a mechanism for delaying evaluation ofform until the expression is in the run-time environment; seeSection 3.2 (Compilation).
Read-only-p designates whether the result can be considered aconstant object. Ift, the result is a read-only quantity that can, if appropriate to theimplementation, be copied into read-only space and/orcoalesced withsimilarconstant objects from otherprograms. Ifnil (the default), the result must be neither copied nor coalesced; it must be considered to be potentially modifiable data.
If aload-time-value expression is processed bycompile-file, the compiler performs its normal semantic processing (such as macro expansion and translation into machine code) onform, but arranges for the execution ofform to occur at load time in anull lexical environment, with the result of thisevaluation then being treated as aliteralobject at run time. It is guaranteed that the evaluation ofform will take place only once when thefile isloaded, but the order of evaluation with respect to the evaluation oftop level forms in the file isimplementation-dependent.
If aload-time-value expression appears within a function compiled withcompile, theform is evaluated at compile time in anull lexical environment. The result of this compile-time evaluation is treated as aliteralobject in the compiled code.
If aload-time-value expression is processed byeval,form is evaluated in anull lexical environment, and one value is returned. Implementations that implicitly compile (or partially compile) expressions processed byeval might evaluateform only once, at the time this compilation is performed.
If thesamelist(load-time-valueform) is evaluated or compiled more than once, it isimplementation-dependent whetherform 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 thesameform is processed byeval orcompile multiple times. Since aload-time-value expression can be referenced in more than one place and can be evaluated multiple times byeval, it isimplementation-dependent whether each execution returns a freshobject or returns the sameobject as some other execution. Users must use caution when destructively modifying the resultingobject.
If two lists(load-time-valueform) that are thesame underequal but are notidentical are evaluated or compiled, their values always come from distinct evaluations ofform. Theirvalues may not be coalesced unlessread-only-p ist.
Examples:
;;; 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)))
Affected By: None.
Exceptional Situations: None.
See Also:
compile-file,compile,eval,Section 3.2.2.2 (Minimal Compilation),Section 3.2 (Compilation)
Notes:
load-time-value must appear outside of quoted structure in a ``forevaluation'' position. In situations which would appear to call for use ofload-time-value within a quoted structure, thebackquotereader macro is probably called for; seeSection 2.4.6 (Backquote).
Specifyingnil forread-only-p 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.