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

Martin Frb lazarus at mfriebe.de
Mon Mar 27 23:11:36 CEST 2023


On 27/03/2023 22:59, Sven Barth via fpc-devel wrote:
> Am 26.03.2023 um 13:30 schrieb Martin Frb via fpc-devel:
>>   TSome = class;
>>   TSome = class(specialize GenLinkedList<integer, TSome>);
>>
>
> The correct way to declare a generic linked list using classes is the 
> following:
>
> === code begin ===
>
> type
>   generic TGenLinkedList<D> = class
>     Data: D;
>     Next: specialize TGenLinkedList<D>;
>   end;
>
>   TSome = specialize TGenLinkedList<Integer>;
>
> === code end ===

Ok, but the forward declaration (which is the only working one of my 
examples) is also important.
Not sure, why it needs the forward. After all, the forward also comes 
after the generic.


   generic TGenLinkedList<D, C> = class
     Data: D;
     Next: C;
   end;

   TSome = class;
   TSome = class(specialize TGenLinkedList<Integer, TSome>)
     other: boolean;
     procedure Foo;
   end;

procedure TSome.Foo;
begin
   Next.other := true; // Next must be type TSome, not just the 
specialized generic.
end;



More information about the fpc-devel mailing list