Sign in Join

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

Mailing lists / fpc-pascal / 2013-09

Is it possible to know the data type of a specialized generics class?

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Xiangrong Fang

Hi All,

say, I have a class TTree<T>; in the destructor of that class, is it possible to know the actual type of T? I want to do things like:

generic TTree<T> = class
public
  Data: T;
  destructor Destroy; override;
end;
destructor TTree.Destroy;
begin
  ... ...
  if Data is TObject then Data.Free;
end;

But "is" does not work with generic types.

Regards,
Xiangrong

Sven Barth
to Xiangrong Fang
Xiangrong Fang wrote 19 lines
Hi All, say, I have a class TTree<T>; in the destructor of that class, is it possible to know the actual type of T? I want to do things like: generic TTree<T> = class public Data: T; destructor Destroy; override; end; destructor TTree.Destroy; begin ... ... if Data is TObject then Data.Free; end; But "is" does not work with generic types.

Short answer: No
Long answer: You could try to use TypeInfo(Data) or TypeInfo(T) and use the returned PTypeInfo to find out the type...

Regards,
Sven

Sven Barth
to Sven Barth
Sven Barth wrote 23 lines
Am 24.09.2013 11:19, schrieb Xiangrong Fang: > Hi All, > > say, I have a class TTree<T>; in the destructor of that class, is it > possible to know the actual type of T? I want to do things like: > > generic TTree<T> = class > public > Data: T; > destructor Destroy; override; > end; > > destructor TTree.Destroy; > begin > ... ... > if Data is TObject then Data.Free; > end; > > But "is" does not work with generic types. > Short answer: No Long answer: You could try to use TypeInfo(Data) or TypeInfo(T) and use the returned PTypeInfo to find out the type...

But it's not a good idea as you'd still need to convert Data to a TObject (e.g. by using a cast), but that will fail if Data is something else than a pointer-like type...

Regards,
Sven

Xiangrong Fang
to Sven Barth
Show quoted text 5 lines
But it's not a good idea as you'd still need to convert Data to a TObject (e.g. by using a cast), but that will fail if Data is something else than a pointer-like type... This worked in my TTree class (destructor):
  if PTypeInfo(TypeInfo(Data))^.Kind = tkObject then TObject(Data).Free;

I don't know in what case it will fail? I tested with a TStringTree, could you please give an example that it will fail?

Sven Barth
to Xiangrong Fang
Xiangrong Fang wrote 11 lines
But it's not a good idea as you'd still need to convert Data to a TObject (e.g. by using a cast), but that will fail if Data is something else than a pointer-like type... This worked in my TTree class (destructor): if PTypeInfo(TypeInfo(Data))^.Kind = tkObject then TObject(Data).Free; I don't know in what case it will fail? I tested with a TStringTree, could you please give an example that it will fail?

Try something that is not a Pointer, e.g. a Shortstring, a record, or an ordinal datatype like QWord.

Regards,
Sven