<div>07.05.2021, 06:09, "Michael Van Canneyt via fpc-devel" <fpc-devel@lists.freepascal.org>:</div><blockquote><p><br /><br />On Fri, 7 May 2021, Sven Barth via fpc-devel wrote:<br /> </p><blockquote><blockquote><br /> I thought it was agreed at the time that this was the most viable way<br /> forward ?<br /> </blockquote><br /> As far as I remember there wasn't really any agreement.</blockquote><p><br />Can be, I was not sure... I remember this path was investigated.<br /> </p><blockquote><blockquote> IIRC there was also the proposal that this could be done automatically<br /> using<br /> a keyword:<br /><br /> var<br />    SomeClass : TSomeClass; dispose;<br /><br /> The compiler can internally create the management record with a single<br /> default<br /> field and the needed management operator, so the user does not need<br /> to create all that.<br /> </blockquote><br /> I'm not aboard with such a keyword. The compiler should provide the<br /> necessary language mechanisms (default field, operator hoisting) and then<br /> there should be a default implementation as part of the RTL. There is no<br /> need to hide this behind a keyword, attribute or whatever.</blockquote><p><br />There is:<br /><br />Using a keyword, the compiler could also optimize things by simply initializing<br />and freeing the instance if the usage of the object in the procedure allows it;<br />it would allow to skip the record and operators and whatnot.<br /><br />I'm not proposing to abolish the record approach, just an additional automatism<br />that will use it, but allows to skip it for simple enough cases which are<br />probably the largest use-case.<br /><br />Don't forget that the record/management operator approach will again blow up<br />binary size; for every variable declared like this you risk creating a new type.<br /><br />The introduction of generics and their abundant use in Delphi has noticably<br />slowed down the compiler and increased binary sizes.<br />To my dismay, compile times of 20 seconds up to 2 minutes have become not<br />uncommon in Delphi. Something almmost unthinkable in D7 days.<br /><br />If that can be avoided by use of a keyword for 90% of use cases,<br />I think it is worth thinking about it and not dismissing it offhand.<br /><br />Michael.<br />_______________________________________________<br />fpc-devel maillist - fpc-devel@lists.freepascal.org<br />https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</p></blockquote><div>I would love to see defer keyword in free pascal, but i think that at some degree we already can emulate it, see:</div><div> </div><div><div><div>program project2;</div><div> </div><div>{$mode objfpc}</div><div>{$modeswitch advancedrecords}</div><div> </div><div>type</div><div>  TDeferedProc = procedure of object;</div><div> </div><div>  TDefer = record</div><div>    fProc: TDeferedProc;</div><div>  public</div><div>    procedure Enqueue(aProc: TDeferedProc);</div><div>    class operator finalize(var aDefered: TDefer);</div><div>  end;</div><div> </div><div>  { TSampleObj1 }</div><div> </div><div>  TSampleObj1 = class</div><div>    procedure Echo;</div><div>    destructor Destroy; override;</div><div>  end;</div><div> </div><div>  TSampleObj2 = class(TSampleObj1);</div><div> </div><div>function Defer: TDefer;</div><div>begin</div><div>  Result.fProc:= nil;</div><div>end;</div><div> </div><div>{ TDefer }</div><div> </div><div>procedure TDefer.Enqueue(aProc: TDeferedProc);</div><div>begin</div><div>  fProc:= aProc;</div><div>end;</div><div> </div><div>class operator TDefer.finalize(var aDefered: TDefer);</div><div>begin</div><div>  aDefered.fProc();</div><div>end;</div><div> </div><div>{ TSampleObj1 }</div><div> </div><div>procedure TSampleObj1.Echo;</div><div>begin</div><div>  WriteLn('Echo from ', ClassName);</div><div>end;</div><div> </div><div>destructor TSampleObj1.Destroy;</div><div>begin</div><div>  Writeln('Freeing ', ClassName);</div><div> </div><div>  inherited;</div><div>end;</div><div> </div><div>procedure MyProc;</div><div>var</div><div>  Obj1: TSampleObj1;</div><div>  Obj2: TSampleObj2;</div><div>begin</div><div>  Obj1:= TSampleObj1.Create;</div><div>  Obj2:= TSampleObj2.Create;</div><div> </div><div>  WriteLn('Objects created');</div><div> </div><div>  Defer.Enqueue(@Obj1.Free);</div><div>  Defer.Enqueue(@Obj2.Free);</div><div> </div><div>  WriteLn('Objects freed defered');</div><div> </div><div>  Obj1.Echo;</div><div>  Obj2.Echo;</div><div> </div><div>  WriteLn('End of MyProc');</div><div>end;</div><div> </div><div>begin</div><div>  MyProc;</div><div>end.</div></div></div><div> </div><div><div><div>-- </div>Luis Lima</div></div>