<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2018-08-18 1:24 GMT+02:00 Ryan Joseph <span dir="ltr"><<a href="mailto:ryan@thealchemistguild.com" target="_blank">ryan@thealchemistguild.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">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%.<br></blockquote><div><br></div><div>You are trying to achieve something what is almost possible with current FPC trunk version (which is also safe in the case of exception):</div><div><br></div><div>=== code begin ===</div><div><div> TAutoCreate<T: class> = record</div><div> _: T;</div><div> class operator Initialize(var a: TAutoCreate<T>);</div><div> class operator Finalize(var a: TAutoCreate<T>);</div><div> class operator Copy(constref a: TAutoCreate<T>; var b: TAutoCreate<T>);</div><div> class operator AddRef(var a: TAutoCreate<T>);</div><div> end;</div><div><br></div><div>class operator TAutoCreate<T>.Initialize(var a: TAutoCreate<T>);</div><div>begin</div><div> a._ := T.Create;</div><div>end;</div><div><br></div><div>class operator TAutoCreate<T>.Finalize(var a: TAutoCreate<T>);</div><div>begin</div><div> a._.Free;</div><div>end;</div><div><br></div><div>class operator TAutoCreate<T>.Copy(constref a: TAutoCreate<T>;</div><div> var b: TAutoCreate<T>);</div><div>begin</div><div> // raise { some exception - prevent copy };</div><div>end;</div><div><br></div><div>class operator TAutoCreate<T>.AddRef(var a: TAutoCreate<T>);</div><div>begin</div><div> // raise { some exception - prevent copy };</div><div>end;</div><div><br></div><div>var</div><div> o: TAutoCreate<TObject>;</div><div>begin // auto Create</div><div> Writeln(o._.ToString); </div></div><div>end. // auto Free;</div></div>=== code end ===</div><div class="gmail_extra"><br></div><div class="gmail_extra">the code above was not tested (written now) but should work. The only disadvantage of above solution is lack of the way to omit "_". </div><div class="gmail_extra"><br></div><div class="gmail_extra">The solution which probably should be accepted by fpc team is default property without indexer:</div><div class="gmail_extra"><br></div><div class="gmail_extra">
<div style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">=== code begin ===</div><div style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">property obj: T read fobj; default;<br></div></div><div class="gmail_extra">
<div class="gmail_extra" style="font-size:small;text-decoration-style:initial;text-decoration-color:initial">=== code end ===</div><br></div><div class="gmail_extra">with this property should be possible:</div><div class="gmail_extra"><br></div><div class="gmail_extra">
<div style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">
<div style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">=== code begin ===</div><div style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">Writeln(o.ToString); <br></div></div><div class="gmail_extra">
<div class="gmail_extra" style="font-size:small;text-decoration-style:initial;text-decoration-color:initial">=== code end ===</div><div class="gmail_extra" style="font-size:small;text-decoration-style:initial;text-decoration-color:initial"><br></div>next you can try to provide compiler magic:<br class="gmail-Apple-interchange-newline"></div></div><div class="gmail_extra"><br></div><div class="gmail_extra">
<div style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">
<div style="text-decoration-style:initial;text-decoration-color:initial">=== code begin ===</div><div style="text-decoration-style:initial;text-decoration-color:initial">var o: TObject auto; // equivalent of var o: TAutoCreate<TObject>;<br></div></div><div style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">// please note that syntax with semicolon <span style="text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">is improper: </span></div><div style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><span style="text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">//var</span>
o: TObject; auto; </div>
<div class="gmail_extra" style="font-size:small;text-decoration-style:initial;text-decoration-color:initial">=== code end ===</div><br class="gmail-Apple-interchange-newline">anyway I am not big fan of "auto" because it means many troubles, what if:</div><div class="gmail_extra"><br></div><div class="gmail_extra">* there is no parameter less constructor</div><div class="gmail_extra">* why to call constructor when there is NewInstance (this seems more proper for this case but...)</div><div class="gmail_extra"><br></div><div class="gmail_extra">after NewInstance you can <span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">(and rather this will be necessary)</span> 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</div><div class="gmail_extra"><br></div><div class="gmail_extra">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).</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div>-- <br></div><div class="gmail_signature"><div dir="ltr"><div>Best regards,<br>Maciej Izak</div></div></div>
</div></div>