[previous] [up] [next]     [contents] [index]
Next: Keywords Up: Global Variable Namespaces Previous: Global Names

Constant Names

A constant name in MzScheme is a global variable name that cannot be changed by define, undefine, or set! once it has been defined. However, constant names can still be used for variables bound by lambda, let, etc.

New constant names are declared with the constant-name procedure, as in (constant-name s). If the symbol s has not been globally defined before constant-name is invoked, then s can still be defined once. Constance is a property of a global variable name (within a single namespace), not a variable's value.

For example:

  (define x 2)
  (define x (box 5)) ; ok
  (constant-name 'x)
  (define x 9) ; exn:misc:constant is raised
  (set! x 5) ; exn:misc:constant is raised
  (let ([x 0]) (set! x 1)) ; ok
  (set-box! x 7) ; ok
  (constant-name 'y) ; assuming y is not yet defined...
  (define y 8) ; ok 

The procedure constant-name-all-globals makes all currently defined names constant. Any number of symbol arguments can be provided to constant-name-all-globals; the global names for these symbols will not be made constant (although they may be constant already).

If a define, set!, or undefine expression attempts to change the value of a constant variable, the exn:misc:constant exception is raised.

A namespace can be created where all built-in procedures and syntax names are constant (see section 9.3).



PLT