[fpc-pascal] Incompatible type for generics?

Sven Barth pascaldragon at googlemail.com
Mon Sep 23 10:00:35 CEST 2013


Am 23.09.2013 09:10 schrieb "Xiangrong Fang" <xrfang at gmail.com>:
>
> Hi All,
>
> I wrote a TTree generic class, the code is here:
>
> https://github.com/xrfang/fpcollection/blob/master/src/units/tree.pas
>
> While using this class, I encountered the following problem:
>
> program project1;
> {$mode objfpc}{$H+}
> uses tree;
> type
>   TIntTree = class(specialize TTree<Integer>)
>   end;
> var
>   ti : TIntTree;
> begin
>   ti := TIntTree.Create(1, nil);
>   ti := ti.Next;  <-- error here
> end.
>
> The error message is:
>
> Error: Incompatible types: got "TIntTree.TTree$LongInt" expected
"TIntTree"
>
> If I do not inherit TTree but just specialize it, there is no error.
>
> How can I solve this problem so that I don' t have to use typecast
everywhere?

Short answer: you can't. The same would happen with normal classes.

Long answer: You could add an additional method "Next" to your "TIntTree"
which returns a "TIntTree" and just do "Result := TIntTree(inherited
Next);" there.

Why are you subclassing in this case anyway?

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130923/8e8e23a0/attachment.html>


More information about the fpc-pascal mailing list