[fpc-devel]Proposal for audo-destructors

KJK::Hyperion noog at libero.it
Mon Sep 13 15:27:10 CEST 2004


At 14.58 12/09/2004, you wrote:
>   procedure CopyFile(SourceFileName, TargetFileName: string);
>   auto
>       s, t: TStream;
>   begin
>       s := TFileStream.Create(SourceFileName, fmOpenRead);
>       t := TFileStream.Create(TargetFileName, fmCreate);
>       t.CopyFrom(s);
>   end;

what do you think about this, instead?

   procedure CopyFile(SourceFileName, TargetFileName: string);
   begin
       TFileStream.Create(TargetFileName, fmCreate).
           CopyFrom(TFileStream.Create(SourceFileName, fmOpenRead));
   end;

I love how C++ can create anonymous objects with the lifetime of the 
expression that invokes their constructor. The "with" construct, moreover, 
lets you perform multiple operations on the temporary object, and, 
borrowing from C#, it can be extended to declare variables in mid-function:

with <var1>: <type1> [ = <init1> ], <var2>: <type2> [ = <init2> ] do [...] 





More information about the fpc-devel mailing list