[fpc-devel] Is this supposed to work (generic)?

Martin Frb lazarus at mfriebe.de
Sun Mar 26 13:30:17 CEST 2023

3.2.3 and 3.3.1 on Win 64bit

Trying a generic linked list.
So the specialized class must have an entry for the "next" element. And 
that entry is of the same type as the class itself.

Now at first, this seems to be not possible using generics, because 
specialize does not allow to pass in the "partially done" class.
(the 2 commented lines produce "Error: Illegal expression")
Only it does work, if the class is forward declared.

So is it supposed to work?
And if it is in the last case, then what about the other two cases?

program Project1;
   generic GenLinkedList<D, T> = class
     Data: D;
     Next: T;

   //TBar = specialize GenLinkedList<integer, TBar>;
   //TFoo = class(specialize GenLinkedList<integer, TFoo>);

   TSome = class;
   TSome = class(specialize GenLinkedList<integer, TSome>);

Btw, it is the same, if the linked list uses actual pointer.

   generic GenLinkedList<D, T> = class
     type  PT = ^T;
     Data: D;
     NextPtr: PT;

More information about the fpc-devel mailing list