[fpc-pascal] Creating capturers

Hairy Pixels genericptr at gmail.com
Wed Nov 1 06:18:49 CET 2023


I'm curious how the capturer is created in the case of anonymous functions. I know the function needs to create a capturer when nested functions are declared but what happens if there is an anonymous function which is declared in the code section?

I think the compiler will only know about the requirement to create a capturer at this point but local variables have already been assigned so do they need to do an extra copy to the capturer when it's created?

With nested function this is not a problem since the capturer can be created before any locals are assigned and thus not copying is needed, if I understand correctly.

======================================

var
  gProc: reference to procedure;

procedure DoThis;
var
  a: array[0..2] of integer;
begin
  // Compiler does not yet know the function requires a capturer
  a := [1,2,3];

  // DoThis requires a capturer now since the function reference is assigned to an anonymous (nested) function.
  // Does it copy the array (and other local variables) or does the compiler always make a capturer for all functions if the "anonymousfunctions" mode switch is enabled?
  gProc := procedure
  begin
    writeln(Length(a));
  end;
end;


Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list