Strange name PTypeInfo for common integer

I need to make a smart discovery of the generic type and return it as a string, but at the moment I don’t understand why delphi put the strange identifier in the PTypeInfo.Name property.

What I have so far:

program SO_29674887;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.TypInfo;

type
  TypeResolver = class
  strict private
    class function ReCast<T>(const AValue) : T;
  public
    class function Format<T>(const AValue : T) : string;
  end;

  Compare = class
  public
    class function IsEqual<A;B>(const AVal : A; BVal : B) : string;
  end;

{ TypeResolver }

class function TypeResolver.ReCast<T>(const AValue): T;
begin
  Result := T(AValue);
end;

class function TypeResolver.Format<T>(const AValue: T): string;
var Info : PTypeInfo;
begin
  Info := TypeInfo(T);

  Result := 'undefined';

  if(Info.Kind = tkInteger) then
    begin
      if(Info.Name = GetTypeName(TypeInfo(Byte))) then
        Result := IntToStr(ReCast<Byte>(AValue))
      else if(Info.Name = GetTypeName(TypeInfo(ShortInt))) then
        Result := IntToStr(ReCast<ShortInt>(AValue))
      else if(Info.Name = GetTypeName(TypeInfo(SmallInt))) then
        Result := IntToStr(ReCast<SmallInt>(AValue))
      else if(Info.Name = GetTypeName(TypeInfo(Integer))) then
        Result := IntToStr(ReCast<Integer>(AValue));
    end;

  Result := Info.Name + ':' + Result;
end;

{ Compare }

class function Compare.IsEqual<A, B>(const AVal: A; BVal: B): string;
begin
  Result := Format('%s = %s', [
    TypeResolver.Format<A>(AVal),
    TypeResolver.Format<B>(BVal)]);
end;

var PAUSE : string;

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    WriteLn(Compare.IsEqual(0, 255));
    WriteLn(Compare.IsEqual(-127, 127));
    WriteLn(Compare.IsEqual(0, 65535));
    WriteLn(Compare.IsEqual(-32768, 32767));
    WriteLn(Compare.IsEqual(0, 4294967295));
    WriteLn(Compare.IsEqual(-2147483648, 2147483647));
    Readln(PAUSE);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

What happens is that instead of giving me the actual type name of an integer, delphi gives me a strange type identifier: 2 ,: 4 ,: 6, etc.

For instance:

Compare.IsEqual(0, 255); //Gives Info.Name = :2, Byte
Compare.IsEqual(-127, 127); //Gives Info.Name = ShortInt, :4
Compare.IsEqual(0, 65535); //Gives Info.Name = :6, Word
Compare.IsEqual(-32768, 32767); //Gives Info.Name = SmallInt, :8
Compare.IsEqual(0, 4294967295); //Gives Info.Name = :01, Cardinal
Compare.IsEqual(-2147483648, 2147483647); //Gives Info.Name = Integer, :21

So my question is how can I find a suitable type for translation, if it seems to me that delphi does not give any hint for the actual type, when does it deliver these identifiers and why does it give these odd identifiers.

+4
source share
2

, .

{$APPTYPE CONSOLE}

uses
  System.TypInfo;

type
  TypeResolver = class
  public
    class function Format<T>(const AValue : T) : string;
  end;

class function TypeResolver.Format<T>(const AValue: T): string;
var
  Info: PTypeInfo;
begin
  Info := TypeInfo(T);
  Result := Info.Name;
end;

begin
  Writeln(TypeResolver.Format(0));
  Writeln(TypeResolver.Format<Byte>(0));
  Writeln(TypeResolver.Format(255));
  Readln;
end.

XE2 :

:3
Byte
Byte

XE6 :

ShortInt
Byte
Byte

, Delphi 0. , . , , Embarcadero , , .

, , , , .

:

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  System.TypInfo;

type
  TypeResolver = class
  public
    class function Format<T>(const AValue : T) : string;
  end;

class function TypeResolver.Format<T>(const AValue: T): string;
var
  Info: PTypeInfo;
  TypeData: PTypeData;
begin
  Info := TypeInfo(T);
  Result := Info.Name;
  if Info.Kind=tkInteger then begin
    TypeData := GetTypeData(Info);
    Result := Result + ', min = ' + IntToStr(TypeData.MinValue) + 
      ', max = ' + IntToStr(TypeData.MaxValue);
  end;
end;

begin
  Writeln(TypeResolver.Format(0));
  Readln;
end.

XE2 :

:3, min = 0, max = 127

XE6 :

ShortInt, min = -128, max = 127

, , generic, , , , XE6.

, , . , , , , .

+3

, TValue, RTTI.pas

TValue.

TMemo :

uses
  RTTI;

type
  TypeResolver = class
  public
    class function ReCast<T>(const AValue): T;
    class function Format<T>(const AValue: T): string;
  end;

  Compare = class
  public
    class function IsEqual<A; B>(const AValA: A; const AValB: B): string;
  end;

class function Compare.IsEqual<A, B>(const AValA: A; const AValB: B): string;
begin
  Result := Format('%s = %s', [TypeResolver.Format<A>(AValA), TypeResolver.Format<B>(AValB)]);
end;

{ TypeResolver }

class function TypeResolver.ReCast<T>(const AValue): T;
begin
  Result := T(AValue);
end;

class function TypeResolver.Format<T>(const AValue: T): string;
begin
  Result := TValue.From(AValue).TypeInfo.Name;
end;

procedure TForm13.FormCreate(Sender: TObject);
begin
  Memo1.Lines.Add(Compare.IsEqual(0, 255));
  Memo1.Lines.Add(Compare.IsEqual(-127, 127));
  Memo1.Lines.Add(Compare.IsEqual(0, 65535));
  Memo1.Lines.Add(Compare.IsEqual(-32768, 32767));
  Memo1.Lines.Add(Compare.IsEqual(0, 4294967295));
  Memo1.Lines.Add(Compare.IsEqual(-2147483648, 2147483647));
  Memo1.Lines.Add(Compare.IsEqual(Form13, Memo1));
end;

, ?

:

ShortInt = Byte
ShortInt = ShortInt
ShortInt = Word
SmallInt = SmallInt
ShortInt = Cardinal
Integer = Integer
TForm13 = TMemo
+4

All Articles