Recent

Author Topic: How do I get the name of a type to use for comparison?  (Read 1614 times)

anonymousstranger

  • New Member
  • *
  • Posts: 49
How do I get the name of a type to use for comparison?
« on: January 17, 2021, 12:18:39 am »
I need to create a procedure in a generic class that selects a method to operate on a variable based on the type that is fed into the parameter once specialized. I have an intuition that RTTI is used for this but I am new to RTTI, so I know precious little on how to implement it. Please help

for example:
Code: Pascal  [Select][+][-]
  1.  
  2. type
  3.   type1=someclasstype;
  4.   type2=anotherclasstype;
  5.   type3=thirdclasstype;
  6.  
  7.  
  8.  generic TGenericClass<T>= class(TObject)
  9. var  
  10.     v:T;
  11.    procedure dothis(var v:T);
  12.    procedure dothat(var v:T);
  13.    procedure dosomething(var v:T);
  14.  
  15.   procedure dowhat(var v:T);
  16. end;
  17.  
  18.   Type1Class=specialize TGenericClass<Type1>
  19.   Type2Class=specialize TGenericClass<Type2>
  20.   Type3Class=specialize TGenericClass<Type3>
  21.  
  22. var
  23.    T1C:Type1Class;
  24.    T2C:Type2Class;
  25.    T3C:Type3Class;
  26.  
  27.  procedure TGenericClass.dowhat(var v:T);
  28.  
  29. begin
  30.   {(pseudocode: how do I do this?)
  31.  
  32.    if T=type1 then
  33.      dothis(v)
  34.   else if T=type2 then
  35.      dothat(v)
  36.   else if T=type3 then
  37.     dosomething(v);
  38.  
  39.   }
  40. end;
  41.  
  42.  

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: How do I get the name of a type to use for comparison?
« Reply #1 on: January 17, 2021, 01:01:40 am »
Trying to ascertain exactly what you are attempting here ?

I think maybe using Generics is not the approach you need, you should be using a  Class base type system where as you can use the built in features of the OOP language...

  If you stay within Class based objects then using lets say the TOBJECT as the base of any derived class you could then use the AS and IS
operators which are RTTI features to determine the class type at runtime.

For example on a normal method ..

 Procedure ParseClass(Var Item:Tobject);
  begin
    If Item is TSomeClass1 Then …
    If Item is TSomeClass2 Then …

etc..

 So create all the classes you want and base them from TOBJECT which will be accepted at the ParseClass method for examination

 using generics to do this is confusing things because you could be passing types like Integers, floats etc and they don't have that kind of info..

I know some more RTTI info is being worked on and at some point you will be able to do this but at what cost for language performance ?

« Last Edit: January 17, 2021, 01:03:38 am by jamie »
The only true wisdom is knowing you know nothing

anonymousstranger

  • New Member
  • *
  • Posts: 49
Re: How do I get the name of a type to use for comparison?
« Reply #2 on: January 17, 2021, 01:41:49 am »
Quote
I think maybe using Generics is not the approach you need

Unfortunately, I do need to use generics for my implementation, because, in so doing, I can manage complexity and redundancy much more efficiently than with the standard object model.

Quote
If you stay within Class based objects then using lets say the TOBJECT as the base of any derived class you could then use the AS and IS operators which are RTTI features to determine the class type at runtime.

If I know that <T> will always be replaced with a class type (and I know this), could I use "as" and "is" anyways?

Alternately I came across the classnameis function. Would this be possible?

Code: Pascal  [Select][+][-]
  1.  
  2. if T.classnameis('type1') then
  3.     dothis(v)
  4. else if T.classnameis('type2') then
  5.      dothat(v)
  6.  else if T.classnameis('type3')then
  7.     dosomething(v);
  8.    
  9.  
« Last Edit: January 17, 2021, 02:01:51 am by anonymousstranger »

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: How do I get the name of a type to use for comparison?
« Reply #3 on: January 17, 2021, 03:32:28 am »
if the compiler does not complain sure...but keep in mind for every different type you declare via generics it generates a complete new set of all the procedures etc …

  This means if you make the generic large with lots of methods those methods are going to be duplicated per type because its obvious they need to deal with the type which is most likely going to be different.

  So you may want to think about this fully if you have some idea of creating large amounts of different types.

 and on top of all that, they are currently per unit which means if you use these generics in other units they get recreated again for the same type.

 just thought I would let you know about that one.

 It was mentioned that duplication from other units is being worked on to prevent that, that will help those like yourself because I don't use G's
The only true wisdom is knowing you know nothing

anonymousstranger

  • New Member
  • *
  • Posts: 49
Re: How do I get the name of a type to use for comparison?
« Reply #4 on: January 17, 2021, 05:08:07 am »
I didn't know that, but, even without knowing that I'm already working on a solution to that problem by putting procedures into libraries that are then referenced by procedural variables  in a generic directory so that the only thing that gets copied is a pointer.  Figured it was better to have multiple references to single procedures than have a full set of procedures in multiple instances.  8-)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How do I get the name of a type to use for comparison?
« Reply #5 on: January 24, 2021, 05:09:26 am »
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2.  
  3. uses typinfo;
  4.  
  5. type
  6.   generic TGen<T> = class
  7.     function Haiyah(P: T): TGen;
  8.   end;
  9.  
  10. function TGen.Haiyah(P: T): TGen;
  11. var
  12.   TI: PTypeInfo;
  13. begin
  14.   TI := TypeInfo(P);
  15.   if      TI = TypeInfo(String)  then WriteLn('String: ',P)
  16.   else if TI = TypeInfo(Integer) then WriteLn('Integer: ',P)
  17.   else                                WriteLn('What the fuck are you?');
  18.   Result := Self;
  19. end;
  20.  
  21. begin
  22.   specialize TGen<String>.Create.Haiyah('Hello').Free;
  23.   specialize TGen<Integer>.Create.Haiyah(65535).Free;
  24.   specialize TGen<Boolean>.Create.Haiyah(true).Free;
  25. end.
  26.  

anonymousstranger

  • New Member
  • *
  • Posts: 49
Re: How do I get the name of a type to use for comparison?
« Reply #6 on: January 24, 2021, 05:48:18 am »
'What the fuck are you?' LOL

Cool, Thanks  :D

anonymousstranger

  • New Member
  • *
  • Posts: 49
Re: How do I get the name of a type to use for comparison?
« Reply #7 on: January 24, 2021, 05:52:44 am »
BTW Will this work even with constructed types?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How do I get the name of a type to use for comparison?
« Reply #8 on: January 25, 2021, 01:01:48 am »
BTW Will this work even with constructed types?
No harm to try ;)

 

TinyPortal © 2005-2018