[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Modifying constants in programs

Here is a paragraph from CLtL which has some bearing on the discussion
of modifying constants in code:

''An additional problem with EQ is that the implementation is permitted to
"collapse" constants (or portions thereof) appearing in code to be
compiled if they are EQUAL.  An object is considered to be a constant in
code to be compiled if it is a self-evaluating form or is contained in a
QUOTE form.  This is why (EQ "Foo" "Foo") might be true or false; in
interpreted code it would normally be false, because reading in the form
(EQ "Foo" "Foo") would construct distinct strings for the two arguments
to EQ, but the compiler might choose to use the same identical string or
two distinct copies as the two arguments in the call to EQ.  Similarly,
(EQ '(A . B) '(A . B)) might be true or false, depending on whether the
constant conses appearing in the QUOTE forms were collapsed by the
compiler.  However, (EQ (CONS 'A 'B) (CONS 'A 'B)) is always false,
because every distinct call to the CONS function necessarily produces a
new and distinct cons.''

This implies that it is illegal to modify a constant in compiled code,
since the compiler is licensed to share all or part of that constant
with other compiled code.

For the particular example of COMPUTE-ONCE, it is not necessary to use a
special variable to hold the computed value.  A lexical variable will
suffice.  The scope of the lexical variable must include the entire
function in which it is referenced.  This is similar to an OWN variable
in Algol 60.