<div dir="ltr"><div class="gmail_default" style="font-family:courier new,monospace">Hi There,<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">While playing with generics (ref. class inheritance and type incompatibility), I have an idea to use "generic class helper" to overcome the problem that generic methods cannot be "virtual" (because the compiler see specialized class different than the generic one?)<br>
<br></div><div class="gmail_default" style="font-family:courier new,monospace">I have a TTree generic class (ref: <a href="https://github.com/xrfang/fpcollection/blob/master/src/units/tree.pas">https://github.com/xrfang/fpcollection/blob/master/src/units/tree.pas</a>), which currently has problem with the Clone method, I did the following:<br>
<br></div><div class="gmail_default" style="font-family:courier new,monospace">type<br></div><div class="gmail_default" style="font-family:courier new,monospace"> generic TTree<T> = class<br> ... ...<br></div><div class="gmail_default" style="font-family:courier new,monospace">
end;<br></div><div class="gmail_default" style="font-family:courier new,monospace"> TTreeType = class of TTree;<br></div><div class="gmail_default" style="font-family:courier new,monospace"> TTreeHelper = class helper for TTreeType;<br>
<br></div><div class="gmail_default" style="font-family:courier new,monospace">The compiler does not compile because it thinks you cannot use a class reference (TTreeType) while defining a class helper. Why? Shouldn't class references has same effect as a Class?<br>
<br></div><div class="gmail_default" style="font-family:courier new,monospace">Ok, since it does not compile, I changed the last line to the following alternatives, but NONE of them compiles:<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">
1) TTreeHelper = class helper for TTree;<br></div><div class="gmail_default" style="font-family:courier new,monospace">2) generic TTreeHelper<T> = class helper for TTree<T>;<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">
My purpose is that since the Clone method cannot be virtual (because descendant class will not have same return type as the generic class), I try to write a class helper for ALL specialized TTree and their descendants.<br>
<br></div><div class="gmail_default" style="font-family:courier new,monospace">Of course I can (and will) follow Sven's advice in that email to modify my TTree class. This email is just two other questions:<br><br>1) is it possible to define class helper for generic classes?<br>
</div><div class="gmail_default" style="font-family:courier new,monospace">2) why class helper require a class and we cannot use a class reference?<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">
Best Regards,<br>Xiangrong<br></div></div>