user401247
user401247

Reputation:

What is the difference between RttiType.TypeKind and RttiType.Name?

What is the semantic difference between:

RttiType.TypeKind and RttiType.Name ?

I ask because couldn't one in principle infer the TypeKind from the Name?

Upvotes: 2

Views: 395

Answers (2)

Ken White
Ken White

Reputation: 125748

RTTIType.Name is a string. RTTI.TypeKind is an enumerated type that is suitable for use in a loop or case statement. They're not the same at all, and "inferring from a string" is not the same thing at all when it comes to actual use. It's much more clear, concise, and efficient to write

case TypeKind of
  tkInteger, tkInt64: DoSomething;
  tkEnumeration: DoThisInstead;
...

than it is to write

if (Name = 'tkInteger') or (Name = 'tkInt64') then
  DoSomething
else if (Name = 'tkEnumeration') then
  DoThisInstead
...

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613461

The TypeKind and Name properties of TRttiType are completely different things.

  • TypeKind tells you what sort of type you have. This can be one of the 23 different options define in the TTypeKind enumerated type.
  • Name tells you which type you have. This is a string and there are an unlimited number of possible values.

Different types will (usually) have different names, but may have the same TypeKind. For example consider this simple demonstration.

program RttiDemo;

{$APPTYPE CONSOLE}

uses
  Rtti;

procedure Main;
var
  Context: TRttiContext;
  TObjectType, TInterfacedObjectType: TRttiType;
begin
  TObjectType := Context.GetType(TObject);
  TInterfacedObjectType := Context.GetType(TInterfacedObject);
  Writeln(TObjectType.Name);
  Writeln(TInterfacedObjectType.Name);
  Assert(TObjectType.TypeKind=TInterfacedObjectType.TypeKind);
end;

begin
  Main;
  Readln;
end.

The output is:

TObject
TInterfacedObject

So, you cannot infer the type kind from the type name since kind and name are quite different things.

Upvotes: 3

Related Questions