[fpc-devel] Defer keyword

J. Gareth Moreton gareth at moreton-family.com
Thu May 6 19:39:11 CEST 2021


In the example given:

   obj := TObject.Create;
   defer objects.Free;

What's wrong with Pascal's existing functionality?

   obj := TObject.Create;
   try
     ...
   finally
     objects.Free;
   end;

If there's a concern about performance penalty, maybe the compiler can 
work something out for simple finally blocks and just copy the code to 
any Exit nodes found rather than calling the pseudo-procedure that a 
try...finally block creates.

Gareth aka. Kit

On 06/05/2021 18:11, Ryan Joseph via fpc-devel wrote:
>
>> On May 6, 2021, at 10:44 AM, Marco van de Voort via fpc-devel <fpc-devel at lists.freepascal.org> wrote:
>>
>> But those types have refcounting built-in and always active. Things like defer don't, which makes that all objects gets refcounting overhead in case somebody needs it for "defer".
>>
>> Contrary to Pascal both the language you reference have garbage collectors, so their objects are already managed anyway,
> The idea of defer isn't necessarily about memory management but rather literally just deferring a statements execution until a predicable point in execution (end of a block or function). Those articles mentioned using them for file IO also so that you can be sure you're going to close the open file handle even if the function returns before. Memory management is just one obvious use case since we have this problem in Pascal often.
>
> I don't think this even affects ref counting of existing types because it all it does it move the statement to the end of the block. Maybe I'm not understanding your point though.
>
> Regards,
> 	Ryan Joseph
>
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>

-- 
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus



More information about the fpc-devel mailing list