[fpc-devel]Proposal for audo-destructors
Vincent Snijders
vsnijders at quicknet.nl
Sun Sep 12 15:05:50 CEST 2004
Nico Aragón wrote:
> Hello,
>
> On Sun, 12 Sep 2004 13:07:24 +0200 (CEST), marcov at stack.nl (Marco van
> de Voort) wrote:
>
>
>>>Absence of automatic construction/destruction of
>>>classes is very painfull and it can significantly
>>>degrade the
>>>development process (i.e. smart pointers and RAII are
>>>impossible).
>>
>>Very painfull is a bit exaggerated :-)
>
>
> Sometimes, it's. F.i.:
>
> procedure CopyFile(SourceFileName, TargetFileName: string);
> var
> s, t: TStream;
> begin
> s := TFileStream.Create(SourceFileName, fmOpenRead);
> try
> t := TFileStream.Create(TargetFileName, fmCreate);
> try
> t.CopyFrom(s);
> finally
> t.Free;
> end;
> finally
> s.Free;
> end;
> end;
Except in the original proposal, constructor were to be compiler
generated too. That is impossible in this example, since the constructor
has parameters.
> Imagine you could write:
>
> procedure CopyFile(SourceFileName, TargetFileName: string);
> auto
> s, t: TStream;
> begin
> s := TFileStream.Create(SourceFileName, fmOpenRead);
> t := TFileStream.Create(TargetFileName, fmCreate);
> t.CopyFrom(s);
> end;
>
> Eight lines instead of sixteen and much clearer. The compiler would
> internally translate it to something like:
>
Vincent.
More information about the fpc-devel
mailing list