[fpc-pascal] Why does nested function get corrupted?

Hairy Pixels genericptr at gmail.com
Fri Sep 9 00:52:01 CEST 2022


Curious more than anything, if is nested captures state in a record and passes it as an hidden self param, why does returning the variable and calling from outside the calling stack frame corrupt the data? It seems like it technically should be there.

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

type
  TProc = procedure is nested;

function TestNested: TProc;
var
  data: integer;
begin
  data := 100;

  result := procedure
  begin
    writeln(data);
  end;
end;

var
  proc: TProc;
begin
  proc := TestNested;
  proc();
end.

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list