Why does the compiler reject the declaration of a 2D generic array?

delphi, generics

Solution

Your code is invalid because you cannot re-declare a generic type as open generic type.

I would declare this as:

type
  TDynMatrix<T> = array of TArray<T>;

This way you still have compatibility that you probably need: the elements of one dimension to a `TArray<T>`.

So then you can write

var
  matrix: TDynMatrix<Integer>;
  values: TArray<Integer>;
....
SetLength(matrix, 2, 2);
values := matrix[0];
values := Copy(matrix[1]);

etc.

Problem

I'd like to declare a type like this: ``` type TDynMatrix<T> = TArray<TArray<T>>; ``` The compiler rejects this with: ``` [dcc32 Error] E2508 Type parameters not allowed on this type ``` I wondered whether the issue was related to the nesting of generics. But it seems not: ``` type TDynArray<T> = TArray<T>;//pointless type I know, but for the sake of the Q ``` also results in the same compiler error. The documentation for the compiler error left me knowing perhaps even less than I knew before I read it: E2508 type parameters not allowed on this type (Delphi) When using class references, you cannot use generic classes directly. You need to use a wrapper class to be able to use generics. ``` program E2508; {$APPTYPE CONSOLE} uses SysUtils; type TMyClass = class end; TMyClassClass<T> = class of TMyClass; begin Writeln('FAIL - E2508 type parameters not allowed on this type'); end. ``` Can anyone explain why I cannot declare generic types in this way?

Original source