> On Nov 2, 2023, at 1:44 PM, Sven Barth <pascaldra...@googlemail.com> wrote:
> 
> Now for nested as well as anonymous routines the compiler determines whether 
> a capturer is required at the point that the nested or anonymous routine is 
> assigned to a function reference (cause otherwise it can be handled as a 
> nested function or even a function or method pointer). This requirement is 
> detected during the parsing of the routine.

On second thought I had some questions about this part. From what I see FPC 
always allocates the interface when reference pointers are used but what you 
write suggests maybe the compiler will demote the type to something more 
optimized (like a nested function). We talked about this at length before but I 
don't think anything was decided yet.

for example:

procedure DoThis;
var
  proc: reference to procedure;
begin
  proc := procedure
          begin
          end;
  proc();
end;

doesn't need the heap allocated instance because the function reference never 
escapes the scope and it has no captured variables so it could be demoted to a 
global function even of nested.

In fact Swift requires you to mark closures types as "@escaping" if they can 
escape the current scope so that the compiler can be optimize them. This is the 
feature I was thinking FPC needs.

Can you clarify this?

Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to