By default typed constants are not constants but so called "writable consts" (lovely oxymoron). Technically they are static lifetime local variables,
By default typed constants are not constants but so called "writable consts" (lovely oxymoron). Technically they are static lifetime local variables, which means variables which are only locally in scope but globally in lifetime and therefore pesist between calls of the same function:Code: Pascal [Select][+][-]function Counter: Integer;const LocalCounter: Integer = 1;begin Result := LocalCounter; // Return current value LocalCounter := LocalCounter + 1; // increment for the next call to be the next valueend; WriteLn(Counter); // 1WriteLn(Counter); // 2WriteLn(Counter); // 3From this perspective, it's quite clear why this does not work, because they are not constants, but variables and therefore their values are dynamic.
I'm porting my programs made in Delphi 7 to Lazarus and, to my surprise, assigning a value to a constant in Delphi 7 is not possible, as you put it above. In Delphi 7 this generates an error during compilation. I really don't like the idea of being able to change the value of a constant. Is it possible to prevent this type of alteration?