为接口变量或参数分配匿名方法?

Max*_*Max 13 delphi closures anonymous-methods

匿名方法本质上interface是一个Invoke方法:

type
  TProc = reference to procedure;

  IProc = interface
    procedure Invoke;
  end;
Run Code Online (Sandbox Code Playgroud)

现在,是否有可能将它们分配给实际的接口变量或将它们作为接口参数传递?

procedure TakeInterface(const Value: IInterface);
begin
end;

var
  P: TProc;
  I: IInterface;
begin
  I := P; // E2010
  TakeInterface(P); // E2010
end;
Run Code Online (Sandbox Code Playgroud)

[DCC32错误] E2010不兼容的类型:'IInterface'和'过程,无类型指针或无类型参数'

问题:这个用例是什么?

有很多对象,不能通过接口引用保持活着.因此,它们被包裹在一个封闭物中并被它摧毁,"智能指针":

type
  I<T> = reference to function : T;

  TInterfaced<T: class> = class (TInterfacedObject, I<T>)
  strict private
    FValue: T;
    function Invoke: T; // Result := FValue;
  public
    constructor Create(const Value: T); // FValue := Value;
    destructor Destroy; override; // FValue.Free;
  end;

  IInterfacedDictionary<TKey, TValue> = interface (I<TDictionary<TKey, TValue>>) end;

  TKey = String;
  TValue = String;

var
  Dictionary: IInterfacedDictionary<TKey, TValue>;
begin
  Dictionary := TInterfaced<TDictionary<TKey, TValue>>
    .Create(TDictionary<TKey, TValue>.Create);
  Dictionary.Add('Monday', 'Montag');
end; // FRefCount = 0, closure with object is destroyed
Run Code Online (Sandbox Code Playgroud)

现在,有时不仅要保持一个单独的对象,还要保持它的上下文.想象一下,你有一个TDictionary<TKey, TValue>和你拉一个枚举出来的:TEnumerator<TKey>,TEnumerator<TValue>TEnumerator<TPair<TKey, TValue>>.或者字典包含并拥有 TObject s.然后,新对象和字典的闭包将进入一个新的闭包,以创建一个单独的独立引用:

type
  TInterfaced<IContext: IInterface; T: class> = class (TInterfacedObject, I<T>)
  strict private
    FContext: IContext;
    FValue: T;
    FFreeObject: Boolean;
    function Invoke: T; // Result := FValue;
  public
    constructor Create(const Context: IContext; const Value: T; const FreeObject: Boolean = True); // FValue = Value; FFreeObject := FreeObject;
    destructor Destroy; override; // if FFreeObject then FValue.Free;
  end;

  IInterfacedEnumerator<T> = interface (I<TEnumrator<T>>) end;

  TValue = TObject; // 

var
  Dictionary: IInterfacedDictionary<TKey, TValue>;
  Enumerator: IInterfacedEnumerator<TKey>;
  Obj: I<TObject>;
begin
  Dictionary := TInterfaced<TDictionary<TKey, TValue>>
    .Create(TObjectDictionary<TKey, TValue>.Create([doOwnsValues]));
  Dictionary.Add('Monday', TObject.Create);

  Enumerator := TInterfaced<
    IInterfacedDictionary<TKey, TValue>,
    TEnumerator<TKey>
  >.Create(Dictionary, Dictionary.Keys.GetEnumerator);

  Obj := TInterfaced<
    IInterfacedDictionary<TKey, TValue>,
    TObject
  >.Create(Dictionary, Dictionary['Monday'], False);

  Dictionary := nil; // closure with object still held alive by Enumerator and Obj.
end;
Run Code Online (Sandbox Code Playgroud)

现在的想法是融化TInterfaced<T>TInterfaced<IContext, T>,这将使用于上下文过时的类型参数(一个接口就足够了),并导致这些consturctors:

constructor TInterfaced<T: class>.Create(const Value: T; const FreeObject: Boolean = True); overload;
constructor TInterfaced<T: class>.Create(const Context: IInterface; const Value: T; const FreeObject: Boolean = True); overload;
Run Code Online (Sandbox Code Playgroud)

作为一个(纯)闭包可能不是人们在使用匿名方法时想到的主要用途.但是,它们的类型可以作为类的接口给出,其对象可以对闭包的破坏进行清理,并TFunc<T>使其能够流畅地访问其内容.虽然,它们不共享共同的祖先,似乎reference to不能将类型的值分配给接口类型,这意味着,没有统一,安全和未来的方式来引用所有类型的闭包以保持它们的存活.

Ste*_*nke 10

这非常简单.我会告诉你两种方式.

var
  P: TProc;
  I: IInterface;
begin
  I := IInterface(Pointer(@P)^);
  TakeInterface(I);
end;
Run Code Online (Sandbox Code Playgroud)

另一种方法是声明PInterface

type
  PInterface = ^IInterface;
var
  P: TProc;
  I: IInterface;
begin
  I := PInterface(@P)^;
  TakeInterface(I);
end;
Run Code Online (Sandbox Code Playgroud)


Dav*_*nan 6

据我所知,你不能用铸造做你需要的.

我想,你可以Move用来做作业:

{$APPTYPE CONSOLE}
type
  TProc = reference to procedure(const s: string);
  IProc = interface
    procedure Invoke(const s: string);
  end;

procedure Proc(const s: string);
begin
  Writeln(s);
end;

var
  P: TProc;
  I: IProc;

begin
  P := Proc;
  Move(P, I, SizeOf(I));
  I._AddRef;//explicitly take a reference since the compiler cannot do so
  I.Invoke('Foo');
end.
Run Code Online (Sandbox Code Playgroud)

我老实说不知道这有多强大.它可以在多个Delphi版本上运行吗?依赖晦涩的无证实施细节是明智的吗?只有您可以确定您所获得的收益是否超过依赖实施细节的负面影响.