Are procedural variables and anonymous functions equivalent if there is no external context?

I understand that there are special actions to maintain the lifetime of an external variable when it is mentioned inside an anonymous procedure. But when an anonymous procedure does not use external variables, does it generate the same assembly call as the old general procedure. In other words, the interiors of the anonymous function in fragment 1 and the NamedFunction from fragment 2 will be the same

Fragment 1

type
  TSimpleFunction = reference to function(x: string): Integer;

begin
  y1 := function(x: string): Integer
    begin
      Result := Length(x);
    end;

  y1('test');
end.

Fragment 1

type
  TWellKnownSimpleFunction = function(x: string): Integer;

function NamedFunction(x: string): Integer;
begin
  Result := Length(x);
end;

var
  y1: TWellKnownSimpleFunction;
begin
  y1:=NamedFunction;

  y1('test');
end.
+3
source share
1 answer

No. Anonymous methods are implemented internally as interface references. Read more on Barry Kelly's article .

, .

- , .

+4

Source: https://habr.com/ru/post/1765110/


All Articles