views:

163

answers:

2

Hi,

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;
+1  A: 

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.

Kornel Kisielewicz
I talking about new RTTI in Delphi 2010 ...are you sure you understood my question ?
Henri Gourvest
@Henri: I did, what I imply is that you don't need RTTI for this task. If you insist on the RTTI interface (why?), then I don't have an idea.
Kornel Kisielewicz
because I'm the author of superobject, the json parser:http://www.progdigy.com/?page_id=6I use RTTI to automatically serialize native types to json.
Henri Gourvest
For array types to get RTTI (using typinfo unit) you can only use dynamic arrays (tkDynArray). All other array types are off.
Ritsaert Hornstra
"tkArray" is available and works, i added a test code in my question
Henri Gourvest
@Henri: And I learn something new every day.
Ritsaert Hornstra
A: 

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;
Robert Love