[fpc-pascal] How to use generics and meta class?

Sven Barth pascaldragon at googlemail.com
Tue Feb 25 09:45:16 CET 2014


Am 25.02.2014 00:27, schrieb silvioprog:
> Hello,
>
> I have this structure:
>
>   generic TMyGeneric<T> = class
>   end;
>
>   TMyGenericClass = class of TMyGeneric;
>
>   TMyType = class
>   end;
>
>   TMyClass1 = specialize TMyGeneric<TMyType>;
>
>   TMyClass2 = class(specialize TMyGeneric<TMyType>)
>   end;
>
> ...
>
> procedure RegisterItem(AItemClass: TMyGenericClass);
> begin
> end;
>
> OK, but when I try:
>
> RegisterItem(TMyClass1);
>
> Return:
>
> Incompatible type for arg no. 1: Got "Class Of TMyGeneric$TMyType", 
> expected "TMyGenericClass".
>
> Or:
>
> RegisterItem(TMyClass2);
>
> Return:
>
> Incompatible type for arg no. 1: Got "Class Of TMyClass2", expected 
> "TMyGenericClass"
>
> So, which syntax to use generic and meta class?

What might work (untested) is this:

=== code begin ===

type
   generic TMyGeneric<T> = class
   public type
     TMyGenericClass = class of TMyGeneric; // in mode Delphi this would 
be "class of TMyGeneric<T>"
   public
     class procedure RegisterItem(aItemClass: TMyGenericClass);
   end;

=== code end ===

Regards,
Sven



More information about the fpc-pascal mailing list