Henri Gourvest
Henri Gourvest

Reputation: 989

no RTTI on unamed data types

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;

Upvotes: 3

Views: 611

Answers (2)

Robert Love
Robert Love

Reputation: 12581

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;

Upvotes: 2

Kornel Kisielewicz
Kornel Kisielewicz

Reputation: 57525

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.

Upvotes: 1

Related Questions