2018-08-18 1:24 GMT+02:00 Ryan Joseph <r...@thealchemistguild.com>: > but is there any merit to this idea if it was cleaned up and made safe? I > did some tests to see if you could prevent passing auto vars out of scope > and it’s possible to prevent most ways but not 100%. >
You are trying to achieve something what is almost possible with current FPC trunk version (which is also safe in the case of exception): === code begin === TAutoCreate<T: class> = record _: T; class operator Initialize(var a: TAutoCreate<T>); class operator Finalize(var a: TAutoCreate<T>); class operator Copy(constref a: TAutoCreate<T>; var b: TAutoCreate<T>); class operator AddRef(var a: TAutoCreate<T>); end; class operator TAutoCreate<T>.Initialize(var a: TAutoCreate<T>); begin a._ := T.Create; end; class operator TAutoCreate<T>.Finalize(var a: TAutoCreate<T>); begin a._.Free; end; class operator TAutoCreate<T>.Copy(constref a: TAutoCreate<T>; var b: TAutoCreate<T>); begin // raise { some exception - prevent copy }; end; class operator TAutoCreate<T>.AddRef(var a: TAutoCreate<T>); begin // raise { some exception - prevent copy }; end; var o: TAutoCreate<TObject>; begin // auto Create Writeln(o._.ToString); end. // auto Free; === code end === the code above was not tested (written now) but should work. The only disadvantage of above solution is lack of the way to omit "_". The solution which probably should be accepted by fpc team is default property without indexer: === code begin === property obj: T read fobj; default; === code end === with this property should be possible: === code begin === Writeln(o.ToString); === code end === next you can try to provide compiler magic: === code begin === var o: TObject auto; // equivalent of var o: TAutoCreate<TObject>; // please note that syntax with semicolon is improper: //var o: TObject; auto; === code end === anyway I am not big fan of "auto" because it means many troubles, what if: * there is no parameter less constructor * why to call constructor when there is NewInstance (this seems more proper for this case but...) after NewInstance you can (and rather this will be necessary) call selected constructor from instance which will be executed like regular method, but what is the sense of "auto" calling NewInstance when you still need to execute Constructor? :D I see rather no reason for this feature in compiler (but nice try :) ). There are many other directions to solve "auto free" problems without compiler modifications - maybe not all can be solved but many problems for sure - for example you have ready to use TAutoFree from mORMot or initial experiments with ARC objects for NewPascal (still worth to mention ;P). -- Best regards, Maciej Izak
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal