The default has been to disable them since Delphi 2 was released, IIRC. The actual option was provided in the dialog with Delphi 1, and I seem to recall a fuss about the default having changed from enabled to disabled in the next version. It's been a long time though, so I could be off by one on when the default was reversed; it may have been D3. Assignable constants are a carry-over from the old Turbo Pascal days, and are a substitute for the lack of an actual static variable type. They were replaced by a better solution, which is initialized (global) variables (see Declaring Variables
).
If you have an actual need to use a writable constant, you should do so only on the smallest (most limited) scope where it's necessary, rather than changing the global setting. Constants should be exactly that where possible (constant). The typical reason for using writable constants is when you require a local variable within a certain scope (such as a procedure, function, or unit) that needs to keep its value between calls, and there is usually a better way to do so. Some of the options are object fields (member variables), or limited scope initialized variables (variables visible in restricted areas, such as within the implementation
section of a unit, and initialized to a starting value).
Assignable constants means the value can be changed at runtime, and is really rarely useful. True constants are just that - constant - and shouldn't be allowed to change.
The same can be said for typed constants; there should be an actual need for using them, and there rarely is unless you're storing a constant array, record, pointer, or procedural type, as in these declarations:
const
TIntLookupArray: array[0..1] of Integer = (1, 2);
TErrorMsgs: array[0..1] of string = ('Invalid input', 'Invalid sequence');
Is there a reason you're using typed constants in the first place? You won't have this problem if you just use
const clicks = 1;
and let the compiler decide the proper type. If you want to make sure it's the size of an Integer
, just use a typecast like
const clicks = Integer(1);
See the Delphi documentation docwiki for more info.