Recent

Author Topic: Generics & RTTI  (Read 3978 times)

Tikani

  • New Member
  • *
  • Posts: 10
Generics & RTTI
« on: August 01, 2017, 11:44:58 pm »
Can I use RTTI inside a generic class, i.e. get a type information about "T"?

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Generics & RTTI
« Reply #1 on: August 02, 2017, 01:34:24 am »
Can I use RTTI inside a generic class, i.e. get a type information about "T"?

In Delphi, you can use TypeInfo(T).  I would imagine FreePascal has an equivalent of that, though its implementation of Generics is a bit different than Delphi's.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Generics & RTTI
« Reply #2 on: August 08, 2017, 02:49:23 am »
There is a TypeInfo function in the TypInfo unit

I was using it another day:

Code: Pascal  [Select][+][-]
  1. TTypeInfo(TypeInfo(S)^).Name;
Be mindful and excellent with each other.
https://github.com/cpicanco/

PascalDragon

  • Hero Member
  • *****
  • Posts: 5448
  • Compiler Developer
Re: Generics & RTTI
« Reply #3 on: August 20, 2017, 01:47:28 pm »
There is a TypeInfo function in the TypInfo unit

I was using it another day:

Code: Pascal  [Select][+][-]
  1. TTypeInfo(TypeInfo(S)^).Name;

Please note that it's a bad idea to store a TTypeInfo value into a variable as quite some information is invisibly attached to the data (e.g. GetTypeData() would provide incorrect values if you'd use it on such a variable). So better use the PTypeInfo type:

Code: Pascal  [Select][+][-]
  1. PTypeInfo(TypeInfo(S))^.Name;

@Tikani: yes, RTTI works as expected in generics (though there might be some bugs here and there, but they should be few and far inbetween :P ). Trunk now even supports the GetTypeKind() intrinsic which directly returns the TTypeKind value of the type or variable as a constant and thus allows for constant folding in if- or case-statements. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14215
  • Probably until I exterminate Putin.
Re: Generics & RTTI
« Reply #4 on: August 20, 2017, 06:02:45 pm »
Trunk now even supports the GetTypeKind() intrinsic which directly returns the TTypeKind value of the type or variable as a constant and thus allows for constant folding in if- or case-statements. :)
Yup since last week and a very welcome addition!
Specialize a type, not a var.

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Generics & RTTI
« Reply #5 on: August 21, 2017, 03:08:45 pm »
There is a TypeInfo function in the TypInfo unit

I was using it another day:

Code: Pascal  [Select][+][-]
  1. TTypeInfo(TypeInfo(S)^).Name;

Please note that it's a bad idea to store a TTypeInfo value into a variable as quite some information is invisibly attached to the data (e.g. GetTypeData() would provide incorrect values if you'd use it on such a variable). So better use the PTypeInfo type:

Code: Pascal  [Select][+][-]
  1. PTypeInfo(TypeInfo(S))^.Name;

@Tikani: yes, RTTI works as expected in generics (though there might be some bugs here and there, but they should be few and far inbetween :P ). Trunk now even supports the GetTypeKind() intrinsic which directly returns the TTypeKind value of the type or variable as a constant and thus allows for constant folding in if- or case-statements. :)

Thank you for pointing that. Lucklly enough, I was not storing anything.
Be mindful and excellent with each other.
https://github.com/cpicanco/

 

TinyPortal © 2005-2018