Writeable typed constants (Delphi)
Go Up to Delphi Compiler Directives (List) Index
Type |
Switch |
Syntax |
{$J+} or {$J-} {$WRITEABLECONST ON} or {$WRITEABLECONST OFF} |
Default |
{$J-} {$WRITEABLECONST OFF} |
Scope |
Local |
The $J directive controls whether typed constants can be modified or not. In the {$J+} state, typed constants can be modified, and are in essence initialized variables. In the {$J-} state, typed constants are truly constant, and any attempt to modify a typed constant causes the compiler to report an error.
Writeable consts refers to the use of a typed const as a variable modifiable at run time. For example:
const foo: Integer = 12; begin foo := 14; end.
With $WRITEABLECONST OFF, this code produces a compile error on the assignment to the foo variable in the begin..end block. To fix it, change the const declaration to a var declaration.
In early versions of Delphi and Object Pascal, typed constants were always writeable, corresponding to the {$J+} state. Old source code that uses writeable typed constants must be compiled in the {$J+} state, but for new applications it is recommended that you use initialized variables and compile your code in the {$J-} state.