Are procedural variable and anonymous function equivalent when there's no outer context?
I understand that there are special actions to maintaining the lifetime of a outer variable when it was mentioned inside an anonymous proce开发者_运维知识库dure. 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.
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.
精彩评论