[fpc-pascal] How to instantiate a public type in a generic class

Sven Barth pascaldragon at googlemail.com
Mon May 18 08:09:05 CEST 2015


Am 17.05.2015 20:46 schrieb "luciano de souza" <luchyanus at gmail.com>:
>
> Hello listers,
>
> Using Freepascal 2.6.4, I have a problem about generics. In short, I
> can't specialize a class with a subtype of a type declared with
> "public type". Let me try to explain.
>
> In dopf.pas, I have:
>
> type
> generic TdGOpf<T1, T2, T3> = class(TdComponent)
> public type
> TEntities = specialize TFPGObjectList<T3>
> end;;
>
> If personmapper is a class specialized from TdGOpf, an instance can be
> created as follows:
>
> Personmapper := TPersonMapper.TEntities.create;
>
> Yes, this code is successul. But this code is not:
>
> type
> generic TGAction <t1, t2> = class(specialize TBrookGAction <t1>);
> private
> Fentities: t2.TEntities; //Error in type definition
> end;
>
> This code failed, so I tried:
>
> type
> generic TGAction <t1, t2> = class(specialize TBrookGAction <t1>);
> public type TCEntities = t2.Tentities; //Error in type definition
> end;
>
> I got also an error if I try:
>
> type
> generic TGAction <t1, t2, t2.TEntities> = class(specialize TBrookGAction
<t1>);
>
> Well, I think the problem is illustrated. The doubt is how can create
> an instance of a public type, in my case, t2.Tentities inside my
> specialized class?

The compiler does not know that T2 can have a subtype called TEntities.
While things like method calls are only handled during specialization,
types are handled when parsing the generic.
In short: that's not possible as is.

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


More information about the fpc-pascal mailing list