views:

82

answers:

1

I understand that there are special actions to maintaining the lifetime of a outer variable when it was mentioned inside an anonymous procedure. But when the anonymous procedure doesn't use outer variables, will it generate the same assembly call as the good old general procedure. In other words, will the internals of the anonymous function in the Fragment 1 and NamedFunction from fragment 2 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.
+4  A: 

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

You can also look at my article where I experimenting with interfaces to mimic anonymous methods.

Anonymous methods are not procedural variables, even if they does not capture variables.

Serg