[fpc-pascal] Two possible generics bugs

Sven Barth pascaldragon at googlemail.com
Sat Nov 30 18:58:02 CET 2019


Am 29.11.2019 um 01:01 schrieb Ryan Joseph via fpc-pascal:
>
> function CopyList (source: TFPSList): TFPSList;
> begin
>    result := TFPSList(source.ClassType.Create);
>    result.Assign(source);
> end;
This can't work. ClassType is of type TClass and TClass.Create calls 
TObject.Create, *not* the constructor of your list type, cause the 
constructor chain is not virtual.

What you can do is this:

=== code begin ===

generic function CopyList<T: TFPSList> (source: T): T;
begin
   result := T.Create;
   result.Assign(source);
end;

var
   a, b: TNodeObjectList;
begin
   a := TNodeObjectList.Create;
   b := specialize CopyList<TNodeObjectList>(a);
end.

=== code end ===

Regards,
Sven


More information about the fpc-pascal mailing list