[fpc-pascal] Writeable typed constants - what's the point?

fpclist at silvermono.co.za fpclist at silvermono.co.za
Thu Jun 18 10:35:54 CEST 2009


On Thursday 18 June 2009 08:51:13 Vincent Snijders wrote:
> Graeme Geldenhuys schreef:
> > Hi,
> >
> > Recently I learned that you can have writeable typed constants. That
> > sounds rather like an oxymoron to my. Writeable constants make no sense,
> > is that then simply a variable? What is the use of a writeable typed
> > constant?
>
> Backwards compatibility with turbo pascal, which lacked initialized
> variables and writable constants were the alternative.

Is this likely to be dropped in future versions of FPC?
I have taken advantage of this in functions where a static variable would be 
used in other languages like C.

In the following example, the typed constant Count is initialised only once 
when Test() is first called and thereafter retains its value between function 
calls. 

procedure Test();
const
  Count : Cardinal = 1;
begin
   Writeln('The value of Count is now ', Count);
   Inc(Count);
end;

var
  i : LongInt;

begin
   for i := 1 to 10 do
      Test();
end.

will yield;

The value of Count is now 1
.
.
The value of Count is now 10

Although a writable constant makes no sense, it come in handy and is a cool 
feature to have. Perhaps a construct derived from this feature can be added 
to FPC?

Regards,
Nino




More information about the fpc-pascal mailing list