Writeable typed constants

 

Type                   Switch

Syntax               {$J+} or {$J-}

{$WRITEABLECONST ON} or {$WRITEABLECONST OFF}

Default               {$J-}

{$WRITEABLECONST OFF}

Scope                Local

 

Remarks

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 runtime. 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 previous versions of Delphi and Borland 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.

 

Compiler directives (list)