<p>Am 12.09.2013 04:03 schrieb "Xiangrong Fang" <<a href="mailto:xrfang@gmail.com">xrfang@gmail.com</a>>:<br>
><br>
> Hi All,<br>
><br>
> I have this generic class:<br>
><br>
> type<br>
> generic TTree<T> = class<br>
> private<br>
> FItems: TList;<br>
> ... ...<br>
> public<br>
> Data: T;<br>
> ... ...<br>
> function FirstChild: TTree;<br>
> ... ...<br>
> end;<br>
><br>
> function TTree.FirstChild: TTree;<br>
> begin<br>
> if FItems.Count = 0 then Exit(nil);<br>
> Exit(TTree(FItems[0]));<br>
> end;<br>
><br>
> when using it, I did:<br>
><br>
> type<br>
> TStringTree = specialize TTree<string>;<br>
><br>
> It generated a compilation error:<br>
><br>
> Error: Incompatible types: got "TTree" expected "TTree$AnsiString"<br>
><br>
> on the line "Exit(TTree(FItems[0]));" of function FirstChild.<br>
><br>
> And I tried to change it to:<br>
><br>
> Exit(TTree<T>(FItems[0]));<br>
><br>
> But is not accepted.<br>
><br>
> How to solve this? thanks.</p>
<p>Could be one of the things I fixed in 2.7.1. Otherwise try the following as a workaround:</p>
<p>=== code begin ===</p>
<p>type<br>
generic TTree<T> = class<br>
public type<br>
TSelfType = TTree;<br>
private<br>
FItems: TList;<br>
... ...<br>
public<br>
Data: T;<br>
... ...<br>
function FirstChild: TSelfType;<br>
... ...<br>
end;</p>
<p>function TTree.FirstChild: TSelfType;<br>
begin<br>
if FItems.Count = 0 then Exit(nil);<br>
Exit(TSelfType(FItems[0]));<br>
end;</p>
<p>=== code end ===</p>
<p>I've not tested it though. Just an idea that it might work.</p>
<p>Regards,<br>
Sven</p>