[fpc-pascal] generic class helper and class references

Xiangrong Fang xrfang at gmail.com
Sun Sep 29 05:30:43 CEST 2013


Hi There,

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?)

I have a TTree generic class (ref:
https://github.com/xrfang/fpcollection/blob/master/src/units/tree.pas),
which currently has problem with the Clone method, I did the following:

type
  generic TTree<T> = class
    ... ...
  end;
  TTreeType = class of TTree;
  TTreeHelper = class helper for TTreeType;

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?

Ok, since it does not compile, I changed the last line to the following
alternatives, but NONE of them compiles:

1) TTreeHelper = class helper for TTree;
2) generic TTreeHelper<T> = class helper for TTree<T>;

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.

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:

1) is it possible to define class helper for generic classes?
2) why class helper require a class and we cannot use a class reference?

Best Regards,
Xiangrong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130929/7a09b1af/attachment.html>


More information about the fpc-pascal mailing list