<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body smarttemplateinserted="true">
<div id="smartTemplate4-template">Hi,<br>
<br>
<p> </p>
<blockquote type="cite"><br>
<br>
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. <br>
</blockquote>
<br>
<p>the classic Delphi way was to use an interface for freeing. It
only requires one type and nothing blows up.<br>
</p>
type<br>
TDefer = class(TInterfacedObject)<br>
toDefer: TObject;<br>
constructor Create(anobject: tobject);<br>
destructor destroy; override;<br>
end;<br>
<br>
constructor TDefer.Create(anobject: tobject);<br>
begin<br>
toDefer := anobject;<br>
end;<br>
<br>
destructor TDefer.destroy;<br>
begin<br>
toDefer.free;<br>
inherited destroy;<br>
end; <br>
</div>
<div><br>
</div>
<div>function deferedFree(obj: TObject): IUnknown;<br>
begin<br>
result := TDefer.Create(obj);<br>
end;<br>
</div>
<div><br>
</div>
<div>Then it can be used as:</div>
<div><br>
</div>
<div>procedure test;<br>
var<br>
sl: TStringList;<br>
begin<br>
sl := TStringList.Create;<br>
deferedFree(sl);<br>
writeln(sl.count);<br>
end;<br>
</div>
<div><br>
</div>
<div><br>
</div>
<div>Unfortunately it fails in FreePascal because the interface is
released too soon. <br>
</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div>At worst, one could use a temporary variable in Delphi:<br>
</div>
<div><br>
</div>
<div>procedure test;<br>
var<br>
sl: TStringList;<br>
begin<br>
sl := TStringList.Create;<br>
var temp := deferedFree(sl);<br>
writeln(sl.count);<br>
end;<br>
</div>
<div><br>
</div>
<div><br>
</div>
<div>
<blockquote type="cite">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. </blockquote>
</div>
<div><br>
</div>
<div>With these generics they copied all the problems of C++. One of
the worst ways of doing that<br>
</div>
<div><br>
</div>
<div>It would have been better to implement them like dynamic
arrays. The generic code gets RTTI, the specialization does not
generate any code, and just avoids explicit casting. <br>
</div>
<div><br>
</div>
<div><br>
Cheers,<br>
Benito </div>
<div class="moz-cite-prefix">On 07.05.21 11:08, Michael Van Canneyt
via fpc-devel wrote:<br>
</div>
<blockquote type="cite"
cite="mid:alpine.DEB.2.22.394.2105071048040.799789@home"> <br>
<br>
On Fri, 7 May 2021, Sven Barth via fpc-devel wrote: <br>
<br>
<blockquote type="cite">
<blockquote type="cite"> <br>
I thought it was agreed at the time that this was the most
viable way <br>
forward ? <br>
<br>
</blockquote>
<br>
As far as I remember there wasn't really any agreement. <br>
</blockquote>
<br>
Can be, I was not sure... I remember this path was investigated. <br>
<br>
<blockquote type="cite">
<blockquote type="cite">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>
<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. <br>
</blockquote>
<br>
There is: <br>
<br>
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. <br>
<br>
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 <br>
probably the largest use-case. <br>
<br>
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. <br>
<br>
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.
<br>
<br>
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.
<br>
<br>
Michael. <br>
_______________________________________________ <br>
fpc-devel maillist - <a class="moz-txt-link-abbreviated"
href="mailto:fpc-devel@lists.freepascal.org">fpc-devel@lists.freepascal.org</a>
<br>
<a class="moz-txt-link-freetext"
href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</a>
<br>
</blockquote>
</body>
</html>