[fpc-pascal] Is it possible to know the data type of a specialized generics class?

Sven Barth pascaldragon at googlemail.com
Tue Sep 24 11:26:59 CEST 2013


Am 24.09.2013 11:25, schrieb Sven Barth:
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130924/977ac325/attachment.html>


More information about the fpc-pascal mailing list