[fpc-pascal]typed constants

Mark Emerson mark at angelbase.com
Thu Jan 2 00:34:21 CET 2003

Michael Van Canneyt wrote:

[questions 1-3 and Michael's responses snipped]

> > 4. If a typed constant is local to a procedure, then I assume it goes on
> > the stack, just as a local var would.  Is this correct?
> No, it does not.
> >
> > 5. If 4 is yes...What value of a local typed constant gets pushed onto
> > the stack?....the original value specified in the const declaration?  I
> > hope this is true, because then the compiler is guaranteeing to
> > initialize it in each new local context.
> Since it it not on the stack: no;
> The matter is subject to discussion: for all practical purposes, the
> 'typed constants' behave as initialized variables, but initialized
> meaning 'initialized at program startup', NOT 'initialized when coming
> into scope'.
> This is how Borland's Delphi compiler handles it, and so for
> compatibility reasons, Free Pascal implements it in the same way.
> A local 'typed constant' is no different from a global one, with the
> exception that it's name is only locally defined.
> Small note: Delphi (and Free Pascal) support 'initialized variables'
> This is simply the same as a 'global constant' which is not declared
> in the constant block, but in the variable block.
> Var
>   S : Ansistring = Nil;
> For this to work in Free Pascal, Delphi compatibility mode must be
> invoked. But, and this is the reason I mention this: Delphi does
> NOT allow initialization of local variables.

It does not compile because of the "nil"...changing it to "abc" works.

> >
> > 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.

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  :)  ].

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.

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

Happy New Year!


More information about the fpc-pascal mailing list