Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-05

How to instantiate a public type in a generic class

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

luciano de souza

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?

Best regards,

Luciano de Souza
Sven Barth
to luciano de souza
"luciano de souza" wrote 39 lines
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

luciano de souza
to luciano de souza: Using Freepascal 2.6.4, I have a problem about generics. In short, I can't specialize a…

Sven,

I really thoght that I had replied your message, but now, I realize I didn't do it.

Actually, the fact compiler does not support the sintax I proposed, it's not very severe. In stead of passing a subtype, I can firstly create a type from the subtype and using it in the specialization:

TPersons = TdgOPFentity.Tentities;

Passing TPersons, the compiler will not complain.

But, say me: is the problem I faced related to 2.4 version? In newer version, would it be possible to use something like I have proposed or will the same behviour be maintained?

2015-05-17 15:46 GMT-03:00, luciano de souza :

Show quoted text 50 lines
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? Best regards, -- Luciano de Souza
Luciano de Souza
Sven Barth
to luciano de souza
"luciano de souza" wrote
But, say me: is the problem I faced related to 2.4 version? In newer version, would it be possible to use something like I have proposed or will the same behviour be maintained?

No, because there is no way for the compiler to know that the type parameter is a type that contains a type names TEntities. While newer versions /do/ support constraints they can not do this (though I do have ideas to extend the capabilities of constraints, but don't expect anything in the near future).

Regards,
Sven