What “type of” declaration represents in Delphi and how can it be used

前端 未结 3 1229
死守一世寂寞
死守一世寂寞 2021-02-01 04:23

There is some strange code in Datasnap.DSReflect unit

  TDSAdapterClassType = type of TDSAdapterClass;

  TDSAdapterClass = class(TPersistent)
  pri         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 05:23

    It seems to be related to PTypeInfo based in the TypeKind as you can write this:

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils;
    
    type
      TIntType = type of Integer;
      TInt64Type = type of Int64;
    
    var
      intType: TIntType;
      int64Type: TInt64Type;
    begin
      try
        intType := Integer;
        Assert(Pointer(intType) = TypeInfo(Integer));
        intType := Cardinal;
        Assert(Pointer(intType) = TypeInfo(Cardinal));
        intType := NativeInt;
        Assert(Pointer(intType) = TypeInfo(NativeInt));
        int64Type := Int64;
        Assert(Pointer(int64Type) = TypeInfo(Int64));
        int64Type := UInt64;
        Assert(Pointer(int64Type) = TypeInfo(UInt64));
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
      Readln;
    end.
    

    But it does not work properly with all types and throws internal compiler errors for some.

提交回复
热议问题