为什么TFunc <t>正确,但TFunc <t>的数组[0..1]不正确?

时间:2015-05-10 21:52:28

标签: delphi generics

如果我有TFunc<T: class>该类型的变量,则允许我直接取消引用类成员。
即使班级完成也支持它。

enter image description here

但是,如果我有一个TFunc<T>的数组,那么这不会编译 为什么不呢?这是编译器错误还是有一些潜在原因?

program Project33;
{$APPTYPE CONSOLE}
uses
  System.SysUtils;

type
  TTestObject = class(TInterfacedObject, IInterface)
    procedure Test;
  end;

procedure TTestObject.Test;
begin
  WriteLn('Test');
end;

procedure Test;
var
  A: IInterface;
  TestObject: array [0..4] of TFunc<TTestObject>;
  SingleObject: TFunc<TTestObject>;
  i: integer;
begin
  for i:= 0 to 4 do begin
    a:= TTestObject.Create;
    TestObject[i]:= function: TTestObject
    begin
      Result:= a as TTestObject;
    end;
    TestObject[i].Test;   //<<-- does not compile
    SingleObject:= TestObject[i];
    SingleObject.Test;   // <<-- works.
  end;
end;

begin
  Test; ReadLn;
end.

0 个答案:

没有答案