[fpc-devel] Declare variables inside the code

Dariusz Mazur darekm at emadar.com
Wed May 11 15:38:52 CEST 2011


  W dniu 2011-05-11 11:39, Joerg Schuelke pisze:
> Am Wed, 11 May 2011 10:21:45 +0200 (CEST)
> schrieb marcov at stack.nl (Marco van de Voort):
>
>> FPC is much lighter on that, and never runs
>> constructors automatically. It only initializes some pointer values
>> to NIL.
>>
>> So you'll have to explain that remark in more detail.
>> _
> Yes, today.
>
> What I mean is that one decision leads to an other.
>
> 1. You decide it is nice to declare variables where you need them.
>
> 2. You find out that it is better practice to define every variable
> where you can initialize it, so that every var fulfills the assertions
> from her type, during the live time .

I agree, but in different way. I'd like see pascal more declarative than 
imperative, and not make statement from declaration.
Initialization  connected with declaration looks good and we can move 
then to the declaration section. For example

procedure foo;
var
   t : tMyObject();
begin
   t.myProc;
end;

that can be equivalent to:

procedure foo;
var
   t : tMyObject;
begin
   t:=tMyObject.create();
   try
     t.myProc;
   finally
     t.free;
   end;
end;


This proposition is not only shorter, more security and possible to 
achieve. There are some things with similar behavior like interfaces, 
strings: they  are initialize before <begin> statement and finalize 
after <end>. Also some gain of speed can be achieve: this object can be 
allocate on stack, which is faster. And second: some virtual calls can 
be resolved and inlining.





-- 
   Darek







More information about the fpc-devel mailing list