[fpc-pascal]typed constants

Michael.VanCanneyt at Wisa.be Michael.VanCanneyt at Wisa.be
Thu Jan 2 10:28:45 CET 2003


On Wed, 1 Jan 2003, Mark Emerson wrote:

> > > I recommend clarifying the matter from questions 4 and 5 in the ref
> > > manual, in your discussion of typed constants, as your discussion (on
> > > page 19) refers to the initialization only "when the progam
> > > starts"....whereas I would like to see the words "when the progam
> > > starts (for a global typed constant) or when the procedure or function is
> > > called (for a local typed constant)".
> >
> > It should be 'when the progam starts' in all cases.
> >
> > I'll bring it to the attention of the other compiler developers;
> > It may be desirable to change this behaviour under certain circumstances.
> >
> > Thank you for pointing out the ambiguity; I will rewrite the part about
> > the local typed constants in the manual, so it is clear what exactly can
> > be expected.
> >
> > Michael.
>
> One of the cardinal rules of programming is to initialize local
> variables...else the results can be unpredictable and hard to debug.
> The concept of having the compiler do this, via a construct such as a
> typed constant (or better yet, a var with an initialization value) is
> something I've wanted for years.  I'm disappointed to hear that this is
> NOT the case with FPC, and that these constructs initialize only at the
> global (i.e. program start) level.

It is also like this in Delphi, this has been tested.

>
> What, pray tell, does that imply for a LOCAL typed constant?  Where is
> the devilish thing stored, if not on the stack?  Does it get stored
> globally?  If so, that means that different (or recursive) invocations
> of the procedure will be operating on this variable in a
> counter-intuitive way.  If this is so, I see no reason to ever place one
> of them in any local declaration [unless I wish to confuse myself  :)  ].

The local typed constant is stored globally.
Although I agree with your reasoning, there is a reason to use them anyway:
Naming scope rules allow you to define a 'temporary' local type.

>
> I recommend to the compiler guys (when schedule permits) implementing
> the compiler initilization of local variables (which have an
> initialization value declared)...that is, it gets initialized when it
> gets pushed onto the stack.

The matter is being discussed by the core members.
The response I've had till now indicate that probably we'll implement it
as initialized variables.

So the following will be allowed

program recurs;

Procedure doit (N : Integer);

Const
  C : Integer = 1;

Var
  A : Integer = 0;

begin
  Writeln('Old value : ',A);
  A:=A+N;
  Writeln('New Value : ',A);
  If N<10 then
    Doit(N+1);
end;

begin
  Doit(1);
end.
-------------------------------------------------

Both the variable A and typed constant C will be initialized at procedure
entry.

Note that Delphi *explicitly* does not allow this. Whether we should
retain the Delphi behaviour in Delphi mode is still a discussion point.

>
> In the meantime, I suggest that the documentation be clarified with
> respect to what is and what is not happening in this matter.

It will be done, as stated earlier.

Michael.





More information about the fpc-pascal mailing list