[fpc-pascal] Managed properties idea

Maciej Izak hnb.code at gmail.com
Mon Oct 9 20:54:09 CEST 2017


2017-10-09 14:43 GMT+02:00 Marcos Douglas B. Santos <md at delfire.net>:

> This TAutoFree just "put" the "finally" and call .Free for an array of
> objects or does it something else?
> Could you send the link of the implementation?
>

"One" and "Several" returns IAutoFree interface. Implementation of that
interface contains TObject/array of TObject fields.  TObject/array of
TObject is freed when interface goes out of the scope (so yes you can omit
"finally", the "Free" call for allocated objects by TAutoFree is
guaranteed). Documentation:

https://synopse.info/files/html/api-1.18/SynCommons.html#TAUTOFREE
https://synopse.info/files/html/Synopse%20mORMot%
20Framework%20SAD%201.18.html#TITL_130

please note that FPC behaves different than Delphi, so you can't do the
Delphi trick:

===code begin===
var
  o: TFoo;
begin
  TAutoFree.One(o, TFoo.Create); // for such code interface is released at
this place in FPC
  WriteLn(o.SomeMethod); // AV i FPC
end; // in Delphi returned interface by TAutoFree.One is released here
===code end===

so correct code for FPC is:

===code begin===
var
  o: TFoo;
begin
  with TAutoFree.One(o, TFoo.Create) do
  begin
    WriteLn(o.SomeMethod);
    o.SomeProperty := 123;
  end;
end;
===code end===

Maybe at some point the first code listing will be valid in FPC too (by
usage of new modeswitch SCOPEDINTERFACEDESTROY).

implementation of TAutoFree can be found here:

https://github.com/synopse/mORMot/blob/master/SynCommons.pas

-- 
Best regards,
Maciej Izak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20171009/4e1013bd/attachment.html>


More information about the fpc-pascal mailing list