Is it safe to type-cast TArray<X> to array of X?

Today I discovered a compiler bug (QC#108577).

The following program fails to compile:

program Project1;

procedure P(M: TArray<TArray<Integer>>);
  SetLength(M, 1, 2);


The compiler gags on the SetLength line and says:

[dcc32 Error] E2029 ')' expected but ',' found

I know I could fix it like this:

procedure P(M: TArray<TArray<Integer>>);
  i: Integer;
  SetLength(M, 1);
  for i := low(M) to high(M) do
    SetLength(M[i], 2);

but naturally I'm keen to avoid having to resort to this.

The following variant compiles and seems to work:

procedure P(M: TArray<TArray<Integer>>);
  TArrayOfArrayOfInteger = array of array of Integer;
  SetLength(TArrayOfArrayOfInteger(M), 1, 2);

I don't know enough about the implementation details of dynamic arrays, TArray<T> casting, reference counting etc. to be confident that this is safe.

Is there anybody out there who does know enough to say one way or another whether or not this will produce the correct code at runtime?


The compiler intrinsic procedure SetLength constructs an array of dimensions on the fly on the stack and calls DynArraySetLength for any dynamic array, be it generic or not. If a generic array wouldn't be structurally compatible with a regular dynamic array, the same implementation for setting the length possibly wouldn't be called.

In fact documentation of DynArraySetLength offers SetLength as an alternative for multi-dimensional arrays. DynArraySetLength could also be used instead of a typecast, but I don't see any reason to prefer one or the other.

Need Your Help

E-Mail with plain text part, html part and attachments

perl email mime html-email email-attachments

I am trying to program a little Perl script which send plain text/html emails with attachments:

Validate so that no special characters are allowed

ruby-on-rails ruby regex validation

How can I validate :title in my model so that only the letters a-z, A-z, and 0-9 are accepted?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.