It's currently not possible to declare a class type for a generic class.
See QC76605 for more information. Also the update below.
Example :
TMyClass<T> = class
end;
TMyClassClass<T> = class of TMyClass<T>; //E2508 type parameters not allowed on this type
The workaround that is presented looks like this :
TMyIntClass = TMyType<Integer>;
TMyIntClassClass = Class of TMyIntClass;
But as commented, that would defeat the whole idea of generics, since the class would have to be subclassed for every generic instantiation.
Here is also a link to a similar workaround on generating a specialized subclass of a generic type: derive-from-specialized-generic-types. In this case it would look like this :
TMySpecialClass = Class(TMyType<Integer>);
Update :
The workaround proposed by RM:
TMyType<T> = class(a.TMyType<T>);
can be implemented with type safety using following scheme:
unit Unita;
interface
type
TMyType<T> = class
Constructor Create;
end;
implementation
uses
Unitb;
constructor TMyType<T>.Create;
begin
Inherited Create;
//WriteLn( Self.QualifiedClassName,' ',Unitb.TMyType<T>.QualifiedClassName);
Assert(Self.QualifiedClassName = Unitb.TMyType<T>.QualifiedClassName);
end;
end.
unit Unitb;
interface
uses Unita;
type
TMyType<T> = class(Unita.TMyType<T>);
implementation
end.
Project Test;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
Unita in 'Unita.pas',
Unitb in 'Unitb.pas';
var
t1 : Unita.TMyType<Integer>;
t2 : Unitb.TMyType<Integer>;
t3 : TMyType<Integer>;
begin
try
//t1 := Unita.TMyType<Integer>.Create; //Exception EAssertionFailed !!
t2 := Unitb.TMyType<Integer>.Create;
t3 := TMyType<Integer>.Create;
ReadLn;
finally
//t1.Free;
t2.Free;
t3.Free;
end;
end.
When creating the generic class, a test is made to check that the created class is derived from the type declared in unit b. Thereby all attempts to create this class from unit a is detected.
Update 2:
Just to be clear, a reference to a generic class, "class of type<T>
" is not possible, but a copy of a generic class is fine.
TMyType<T>
to refer from unit b? In that case your workaround might be feasible. – Thier