07.05.2021, 06:09, "Michael Van Canneyt via fpc-devel" <fpc-devel@lists.freepascal.org>:



On Fri, 7 May 2021, Sven Barth via fpc-devel wrote:
 


 I thought it was agreed at the time that this was the most viable way
 forward ?
 

 As far as I remember there wasn't really any agreement.


Can be, I was not sure... I remember this path was investigated.
 

 IIRC there was also the proposal that this could be done automatically
 using
 a keyword:

 var
    SomeClass : TSomeClass; dispose;

 The compiler can internally create the management record with a single
 default
 field and the needed management operator, so the user does not need
 to create all that.
 

 I'm not aboard with such a keyword. The compiler should provide the
 necessary language mechanisms (default field, operator hoisting) and then
 there should be a default implementation as part of the RTL. There is no
 need to hide this behind a keyword, attribute or whatever.


There is:

Using a keyword, the compiler could also optimize things by simply initializing
and freeing the instance if the usage of the object in the procedure allows it;
it would allow to skip the record and operators and whatnot.

I'm not proposing to abolish the record approach, just an additional automatism
that will use it, but allows to skip it for simple enough cases which are
probably the largest use-case.

Don't forget that the record/management operator approach will again blow up
binary size; for every variable declared like this you risk creating a new type.

The introduction of generics and their abundant use in Delphi has noticably
slowed down the compiler and increased binary sizes.
To my dismay, compile times of 20 seconds up to 2 minutes have become not
uncommon in Delphi. Something almmost unthinkable in D7 days.

If that can be avoided by use of a keyword for 90% of use cases,
I think it is worth thinking about it and not dismissing it offhand.

Michael.
_______________________________________________
fpc-devel maillist - fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

I would love to see defer keyword in free pascal, but i think that at some degree we already can emulate it, see:
 
program project2;
 
{$mode objfpc}
{$modeswitch advancedrecords}
 
type
  TDeferedProc = procedure of object;
 
  TDefer = record
    fProc: TDeferedProc;
  public
    procedure Enqueue(aProc: TDeferedProc);
    class operator finalize(var aDefered: TDefer);
  end;
 
  { TSampleObj1 }
 
  TSampleObj1 = class
    procedure Echo;
    destructor Destroy; override;
  end;
 
  TSampleObj2 = class(TSampleObj1);
 
function Defer: TDefer;
begin
  Result.fProc:= nil;
end;
 
{ TDefer }
 
procedure TDefer.Enqueue(aProc: TDeferedProc);
begin
  fProc:= aProc;
end;
 
class operator TDefer.finalize(var aDefered: TDefer);
begin
  aDefered.fProc();
end;
 
{ TSampleObj1 }
 
procedure TSampleObj1.Echo;
begin
  WriteLn('Echo from ', ClassName);
end;
 
destructor TSampleObj1.Destroy;
begin
  Writeln('Freeing ', ClassName);
 
  inherited;
end;
 
procedure MyProc;
var
  Obj1: TSampleObj1;
  Obj2: TSampleObj2;
begin
  Obj1:= TSampleObj1.Create;
  Obj2:= TSampleObj2.Create;
 
  WriteLn('Objects created');
 
  Defer.Enqueue(@Obj1.Free);
  Defer.Enqueue(@Obj2.Free);
 
  WriteLn('Objects freed defered');
 
  Obj1.Echo;
  Obj2.Echo;
 
  WriteLn('End of MyProc');
end;
 
begin
  MyProc;
end.
 
-- 
Luis Lima
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to