匿名方法基本上是interface
个Invoke
方法:
type
TProc = reference to procedure;
IProc = interface
procedure Invoke;
end;
现在,是否可以将它们分配给实际的接口变量或将它们作为接口参数传递?
procedure TakeInterface(const Value: IInterface);
begin
end;
var
P: TProc;
I: IInterface;
begin
I := P; // E2010
TakeInterface(P); // E2010
end;
[DCC32错误] E2010不兼容的类型:'IInterface'和'过程,无类型指针或无类型参数'
有很多对象,不能通过接口引用保持活着。因此,它们被包裹在一个闭包中并被它摧毁,"Smart Pointers":
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
现在,有时不仅要保持一个单独的对象,还要保持它的上下文。想象一下,您有一个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;
现在的想法是融合TInterfaced<T>
和TInterfaced<IContext, T>
,这将使上下文的类型参数过时(接口就足够了)并导致这些构造函数:
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;
作为(纯)闭包可能不是使用匿名方法时想到的主要用法。但是,它们的类型可以作为类的接口给出,其对象可以对闭包的破坏进行清理,而TFunc<T>
使它能够流畅地访问其内容。虽然,它们不共享共同的祖先,似乎reference to
类型的值不能分配给接口类型,这意味着,没有统一,安全和未来的方式来引用所有类型的闭包来保持它们活着。
答案 0 :(得分:10)
这非常简单。我会告诉你两种方式。
var
P: TProc;
I: IInterface;
begin
I := IInterface(Pointer(@P)^);
TakeInterface(I);
end;
另一种方法是声明PInterface
type
PInterface = ^IInterface;
var
P: TProc;
I: IInterface;
begin
I := PInterface(@P)^;
TakeInterface(I);
end;
答案 1 :(得分: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.
我老实说不知道这有多强大。它可以在多个Delphi版本上运行吗?依赖晦涩的无证实施细节是明智的吗?只有您可以确定您所获得的收益是否超过了依赖实施细节的负面影响。