How do I get the name of a type to use for comparison? lazarus / 21-General ---------------------------------------------------------------------- anonymousstranger · 2021-01-16 23:18 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  type  type1=someclasstype;  type2=anotherclasstype;  type3=thirdclasstype;   generic TGenericClass= class(TObject)var   v:T;   procedure dothis(var v:T);   procedure dothat(var v:T);   procedure dosomething(var v:T);   procedure dowhat(var v:T);end;   Type1Class=specialize TGenericClass  Type2Class=specialize TGenericClass  Type3Class=specialize TGenericClass var   T1C:Type1Class;   T2C:Type2Class;   T3C:Type3Class;  procedure TGenericClass.dowhat(var v:T); begin  {(pseudocode: how do I do this?)    if T=type1 then     dothis(v)  else if T=type2 then     dothat(v)  else if T=type3 then    dosomething(v);   }end; ---------------------------------------------------------------------- jamie · 2021-01-17 00:01 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 ? ---------------------------------------------------------------------- anonymousstranger · 2021-01-17 00:41 QuoteI 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. QuoteIf 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 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  if T.classnameis('type1') then    dothis(v)else if T.classnameis('type2') then     dothat(v) else if T.classnameis('type3')then    dosomething(v); ---------------------------------------------------------------------- jamie · 2021-01-17 02:32 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 ---------------------------------------------------------------------- anonymousstranger · 2021-01-17 04:08 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. ---------------------------------------------------------------------- Leledumbo · 2021-01-24 04:09 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc} uses typinfo; type  generic TGen = class    function Haiyah(P: T): TGen;  end; function TGen.Haiyah(P: T): TGen;var  TI: PTypeInfo;begin  TI := TypeInfo(P);  if      TI = TypeInfo(String)  then WriteLn('String: ',P)  else if TI = TypeInfo(Integer) then WriteLn('Integer: ',P)  else                                WriteLn('What the fuck are you?');  Result := Self;end; begin  specialize TGen.Create.Haiyah('Hello').Free;  specialize TGen.Create.Haiyah(65535).Free;  specialize TGen.Create.Haiyah(true).Free;end. ---------------------------------------------------------------------- anonymousstranger · 2021-01-24 04:48 'What the fuck are you?' LOL Cool, Thanks ---------------------------------------------------------------------- anonymousstranger · 2021-01-24 04:52 BTW Will this work even with constructed types? ---------------------------------------------------------------------- Leledumbo · 2021-01-25 00:01 Quote from: anonymousstranger on January 24, 2021, 05:52:44 amBTW Will this work even with constructed types? No harm to try