Immutability¶
How do I make const
variables constant?¶
Add a global directive {$J-}
to your main pascal file.
Why is const
section writeable in by default?¶
In Turbo Pascal 7.0, you could only assign values in the const
section, not in var
section.
As the official docs mentions, by default const
is writeable (that is {$J+}
) to keep Turbo Pascal compatibility.
It's a relic of old Delphi and Object Pascal and thus
{$J+}
is the default to allow old programs to compile, but it isn't required anymorechsh -s /usr/bin/pwsh — 13/12/2023 12:09
Consider the following program. By the default {$J+}
, you can change the gravity of the Earth, with the gravity of the Moon!
To make const
variables behaving like const
variables in modern programming languages, use {$J-}