[fpc-devel] potential bug, generics vs helpers

Kornel Kisielewicz kornel.kisielewicz at gmail.com
Fri Jan 27 17:11:02 CET 2012

On Fri, Jan 27, 2012 at 4:54 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
> Am 27.01.2012 16:23, schrieb Kornel Kisielewicz:
>> we define TA and TB in a different units it doesn't work anymore --
>> which isn't surprising. So either the manual should be updated, or the
>> "reusage" of the generic should be removed.
> In my opinion the documentation should be updated, because in Delphi (and
> FPC's mode Delphi in trunk) you use code like the following:
> procedure Foo;
> var
>  A: TList<Pointer>;
>  B: TList<Pointer>;
> begin
>  A := TList<Pointer>;
>  B := A;
> end;

Does it also work (in Delphi and in FPC Delphi mode) if A and B would
be defined in different units?

If I understand generics correctly, this is not so simple -- in which
object file does TList<Pointer> if it is used in two different units?
It shouldn't go to the object file of the unit with TList, because it
might not be used in a different compilation. It should also not go
into either unit because that would make them depend on each other...

>> ... then TA and TB are treated as separate types, hence each has it's
>> own helper -- program outputs "AB".
> This is interesting. In my opinion (under the aspect that TA = TB currently)
> it should print "BB" in both cases. I'll need to investigate this.
> Also the case that both spezializations in the same unit are treated the
> same, but aren't in the other one. Also something I'll need to investigate
> (also in FPC 2.7.1).

Well but this would introduce the need to either duplicate the
generated code for each unit, or keeping in the TList unit object, or
do I understand the compilation process incorrectly?

>> 2) In the second program, is it OK that the compiler silently discards
>> the first Helper class, or maybe should it produce an error? Note that
>> this also works for non-generic records (what does Delphi do?)
> As I said above: given that TA is considered equal to TB it should print the
> same letter in both cases.

So multiple helpers for a single class are allowed? And the order in
which they appear defines which overloaded names are used?

>> 3) (most important for me) this all was discovered because I wanted to
>> find a way to have two different "flavors" of a base record, extended
>> by different helpers (eg. Vector4D base class extended to a
>> Quaternion, and a Color) -- using generics provided a easy way to make
>> a base record "evolve" into two distinct types, so in one unit I have
>> TGLColor4 = specialize TVector4<  Single>; with a TGGColor4Helper and
>> in another I have TQuaternion = specialize TVector4<Single>  with a
>> TQuaternionHelper). I really like this solution, but depending on the
>> way the above may change, I don't know if it won't be "fixed" not to
>> work anymore in a later version of FPC?
> Currently they will sometimes be the same and sometimes not. This would be
> ok if we wouldn't have helpers, but as we have helpers now as well a
> solution for this needs to be found.

In this case, would there be another possibility to differentiate base
objects for helpers? As in manually force the compiler to treat types
differently? This would be extremely useful in a case like mine. I
tried "type TBRecord = type TARecord;" but that also didn't work.
Kornel Kisielewicz

More information about the fpc-devel mailing list