开发者

no RTTI on unamed data types

开发者 https://www.devze.com 2022-12-17 08:38 出处:网络
AFAIK the compiler does not generate RTTI if the type is not named. eg: T = 开发者_高级运维array[0..1,0..1] of Integer;

AFAIK the compiler does not generate RTTI if the type is not named. eg: T = 开发者_高级运维array[0..1,0..1] of Integer; In this case it is possible to know the total size of the array, but it is impossible to know the size of each dimension.

It works only if I use a type explicitly named: T01 = 0..1; T = array[T01,T01] of Integer;

I missed something?

Test code:

type
  t = array[0..1, 0..1] of Integer;

procedure test;
var
  i: PTypeInfo;
  d: TArrayTypeData;
begin
  i := TypeInfo(t);
  assert(i.Kind = tkArray);
  d := GetTypeData(i).ArrayData;
end;


You can still get array dimensions using builtins High and Low. Let's take the example type array[0..1,3..4] of Integer:

Low(T) // low bound of first range (0)
High(T) // high bound of first range (1)
Low(T[Low(T)]) // low bound of second range (3)
High(T[Low(T)]) // high bound of second range (4)

In the latter two, you can use any valid index in the index value.


Yes this is currently limitation of the RTTI information generated, you must have a type name.

Things like this will not work:

var
 StrArray :  Array of String;

But the following will work:

type
  TStrArray = Array of String;
var
  StrArray : TStrArray;

I typically have switched my switched my dynamic arrays to use the new syntax of TArray which is defined in the system.pas unit as, to make sure they do have names.

TArray<T> = array of T;

So a workaround to your specific problem would be to declare a type name for that array.

type
  TMyArray = array[0..1, 0..1] of Integer;
var
  t : TMyArray;
0

精彩评论

暂无评论...
验证码 换一张
取 消