[fpc-pascal] Re: Use generic class as parameter (was: how to write enumerator for trees)

Sven Barth pascaldragon at googlemail.com
Sun Mar 24 11:07:13 CET 2013


On 24.03.2013 03:49, Xiangrong Fang wrote:
> I think the problem is not fixed in 2.7.1:
>
> xrfang at laptop ~/fpc $ ./compiler/ppcx64 -n -Furtl/units/x86_64-linux
> -Fupackages/fcl-base/units/x86_64-linux -viwn -FE/home/xrfang -Sc demo2.lpr
> Target OS: Linux for x86-64
> Compiling demo2.lpr
> Compiling treap.pas
> demo2.lpr(88,11) Error: Incompatible types: got "TTreap$2$crc94C56373"
> expected "TRoller"
> demo2.lpr(98) Fatal: There were 1 errors compiling module, stopping
> Fatal: Compilation aborted

Aha! Now that I've seen your code I know where the problem is.

Your TRoller class is declared like this:

=== code begin ===

type
   TRoller = class(specialize TTreap<PPerson, Byte>)
     //...
   end;

=== code end ===

So TRoller is a child class of TTreap<PPerson, Byte> and thus the result 
type of Copy is TTreap<PPerson, Byte>. This is basically not related to 
generics, but to normal class inheritance. To solve this you need to 
adjust your TTreap.Copy in such a way that it doesn't use 
"TTreap.Create", but "ClassType.Create" and you must use TRoller's Copy 
then this way:

=== code begin ===

r2 := r1.Copy as TRoller;

=== code end ===

Regards,
Sven



More information about the fpc-pascal mailing list