What you have there is a typed constant. Typed constants are distinct from ordinary constants (a.k.a. true constants), which is what your constVar
is. Notice how you didn't need to specify a type on constVar
; if you had, you may see that the compiler allows you to assigned new values to it, too:
const
constVar: Integer = 1;
The Free Pascal manual describes typed constants:
Contrary to ordinary constants, a value can be assigned to them at run-time. This is an old concept from Turbo Pascal, which has been replaced with support for initialized variables: For a detailed description, see section 4.4, page 183.
Support for assigning values to typed constants is controlled by the {$J}
directive: it can be switched off, but is on by default (for Turbo Pascal compatibility). Initialized variables are always allowed.
For an initialized variable, replace const
with var
in your declaration. It will get its value upon entering scope. Or, turn off the $J
directive prior to the typed-constant declaration:
{$J-}
const
constArr: array [1..3] of ShortInt = (1, 2, 3);
{$J+}
Whether you turn it back on afterward is up to you.
Typed constants are modifiable because of the way they're stored in memory. In fact, it's because they're stored in memory that they were originally modifiable. Ordinary constants aren't stored in memory as distinct objects. When the compiler encounters an ordinary constant used in your program, it replaces it in-line with the constant's value, just as though you'd used a literal in its place. A typed constant, on the other hand, resides in its own location in memory, and when you refer to one in code, its value is read from memory, just like using any other variable. You use typed constants when there is no syntax available for a literal — you can't have array or record literals, for instance.