2010-09-16 10 views
3

我明白,在匿名程序中提到它时,有特殊的操作来维护外部变量的生命周期。但是,当匿名过程不使用外部变量时,它是否会生成与旧的一般过程相同的程序集调用。换言之,将匿名函数的在片段1和NamedFunction从片段2的内部是相同当没有外部上下文时,过程变量和匿名函数是否等价?

片段1

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

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

    y1('test'); 
end. 

片段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

号匿名方法是作为接口引用在内部实现。详情请阅读Barry Kelly's article

您也可以查看my article,在那里我尝试使用接口来模拟匿名方法。

匿名方法不是过程变量,即使它们不捕获变量。

相关问题