[fpc-devel]Proposal for audo-destructors

Michael.VanCanneyt at Wisa.be Michael.VanCanneyt at Wisa.be
Sun Sep 12 15:28:40 CEST 2004


On Sun, 12 Sep 2004, Vincent Snijders wrote:

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

It is not so easy. What about the following code:

   procedure CreateIOStream(Var AStream : TStream);
   auto
      t: TStream;
   begin
      s := TFileStream.Create(SourceFileName, fmOpenRead);
      // Something
      AStream:=S;
   end;

Here the assignment is explicit, but it could be implicit as well.
It's not possible to solve this kind of issue without some form 
of reference counting.

The matter has been discussed several times by the FPC core group, at great
length. No satisfying solution on which everyone agreed was reached. 

Michael.




More information about the fpc-devel mailing list