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

How constant are defconstants?



The Manual, page 68:
   DEFCONSTANT is  like  DEFPARAMETER  but  does  assert  that the value
   of the variable "name" is fixed and  does  license  the  compiler  to
   build assumptions  into  programs  being  compiled.  (However, if the
   compiler chooses to replace references to the name  of  the  constant
   by the  value  of  the  constant  in  code to be compiled, perhaps in
   order to allow further optimization,  the  compiler  must  take  care
   that such  "copies" appear to be EQL to the object that is the actual
   value of the constant.  For example, the  compiler  may  freely  make
   copies of  numbers  but must exercise care when the value is a list.)

Suppose I  say
(DEFCONSTANT *PLANETS*
   '(MERCURY VENUS EARTH MARS JUPITER SATURN URANUS NEPTUNE PLUTO))
[and assume I got those in the  right  order].   It  is  now  presumably
an error to say
                           (SETQ *PLANETS* x)
but what about
                       (SETF (CAR *PLANETS*) x) ?
It would  seem  problematic,  since  the  compiler might want to compile
(CAR *PLANETS*) or (NTH 0 *PLANETS*)  as  'MERCURY,  just  as  it  might
want to compile (CAR '(A B C)) as 'A.

For simple constants like that, the SETF seems unreasonable, but  what's
a good  way  to  create  an  array  that is very expensive to initialize
(i.e., where computing the initial values takes a long time), and  which
should   be   "read-only"   thereafter  (a  la  DEFSTRUCT)?   I'm  using
               (DEFCONSTANT *THE-DATA* (MAKE-ARRAY ...))
followed by lots of
            (SETF (AREF *THE-DATA* ...) <long computation>)
forms, but that  looks  strange.   I'd  like  constant  references  into
*THE-DATA* to  be  replaced  by the appropriate values, if possible, but
I'm really more interested in the read-only nature of the array  itself.
-------