[fpc-pascal] Static local variables available?
Santiago A.
svaa at ciberpiula.net
Fri Jul 21 09:50:09 CEST 2017
El 20/07/2017 a las 15:50, Sven Barth via fpc-pascal escribió:
>
> Am 20.07.2017 13:01 schrieb "Bo Berglund" <bo.berglund at gmail.com
> <mailto:bo.berglund at gmail.com>>:
> >
> > On Thu, 20 Jul 2017 11:11:50 +0200, Maciej Izak
> > <hnb.code at gmail.com <mailto:hnb.code at gmail.com>> wrote:
> >
> > >2017-07-20 11:03 GMT+02:00 Bo Berglund <bo.berglund at gmail.com
> <mailto:bo.berglund at gmail.com>>:
> > >
> > >> So since I don't really want to use a global, is it possible to
> > >> declare a local variable static in the sense that it retains its
> > >> values across calls to the procedure?
> > >> If so how is it done?
> > >>
> > >
> > >procedure foo;
> > >{$PUSH}
> > >const{$J+}
> > > s : string ='';
> > >{$POP}
> >
> > Thanks,
> > but it looks a bit involved, probably better to use an object field
> > variable instead only accessible from the internal methods.
>
> If you don't want to use $push/$pop then you can also simply enable
> $J+ for the whole unit. But this will also mean that global constants
> are writable.
>
Well, I'm an old dog so I prefer old fashion ways. A variable that
retains its value is, from memory point of view, a global variable. I
mean, the memory is never freed, it's not freed when it goes out of
scope. So, the problem is what to do to limit the visibility to the
procedure.
What about the old interface/implementation ways? You can't limit the
visibility to the procedure, but you can limit the visibility to the
implementation, so it is globally invisible.
unit hiddenVar;
interface
procedure foo;
procedure resetFoo;
implementation
var
HiddenValue:integer;
procedure foo;
begin
writeln(HiddenValue);
inc(HiddenValue);
end;
procedure resetFoo;
begin
HiddenValue:=0;
end;
initialization
resetFoo;
end.
--
Saludos
Santiago A.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170721/a062f77e/attachment.html>
More information about the fpc-pascal
mailing list