[fpc-pascal] Creating capturers

Hairy Pixels genericptr at gmail.com
Fri Nov 3 02:00:06 CET 2023



> On Nov 2, 2023, at 1:44 PM, Sven Barth <pascaldragon at 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



More information about the fpc-pascal mailing list