In Delphi method signatures, why can't Generics be combined with arrays?

144 Views Asked by At

While attempting to revise one of my records to use Generics, I discovered that combining Generics and arrays within one of the methods' signatures produces an error:

Type [type] is not yet completely defined

I have two minimal examples. The first is for parameter types:

  TRec1 = record
    procedure DoFooList(A: TArray<TRec1>);
  end;

  TGeneRec1<T> = record
    procedure DoFoo(G: TGeneRec1<T>);
    procedure DoFooList(A: TArray<TGeneRec1<T>>); // This causes an error.
  end;

The second is for return types:

  TRec2 = record
    function ReturnFooList: TArray<TRec2>;
  end;

  TGeneRec2<T> = record
    function ReturnFoo: TGeneRec2<T>;
    function ReturnFooList: TArray<TGeneRec2<T>>; // This causes an error.
  end;

In each example, each type within a method's signature can involve an array or a Generic, but not both.

My have a few questions about this phenomenon:

  • Is there a technical reason why combining these features is impossible?
  • If not, then have the developers explained why it is unsupported?
  • Why is the error characterized as "not yet completely defined"? (That's not a sufficient condition for the error; it's true regardless of whether the type is Generic and regardless of whether it's an array.)
  • Is there a way to circumvent this restriction?
1

There are 1 best solutions below

0
Trome On

the generics type is prepared during compiling . when the compiler arrive at DoFooList generate a DoFooList function for every type used in the code with this record. cannot prepare a function with undefined type . should be everything defined at moment of compiling.

TGeneRec1<T> = record 
  procedure DoFoo(G: TGeneRec1<T>);
  procedure DoFooList<T1 : record>(A: TArray<T1>); 
end;

will work if use it like this :

procedure example
var 
  lExVar : TGeneRec1<Type>; 
  lExArr : TArray<TGeneRec1<Type>>
begin
  lExVar.DoFooList<TGeneRec1<Type>>(lExArr );   
end;