今天我发现了一个编译器错误(QC#108577 http://qc.embarcadero.com/wc/qcmain.aspx?d=108577).
以下程序无法编译:
program Project1;
{$APPTYPE CONSOLE}
procedure P(M: TArray<TArray<Integer>>);
begin
SetLength(M, 1, 2);
end;
begin
end.
编译器对此嗤之以鼻SetLength
线并说:
[dcc32 Error] E2029 ')' expected but ',' found
我知道我可以这样修复它:
procedure P(M: TArray<TArray<Integer>>);
var
i: Integer;
begin
SetLength(M, 1);
for i := low(M) to high(M) do
SetLength(M[i], 2);
end;
但我自然希望避免诉诸于此。
以下变体可以编译并且似乎可以工作:
procedure P(M: TArray<TArray<Integer>>);
type
TArrayOfArrayOfInteger = array of array of Integer;
begin
SetLength(TArrayOfArrayOfInteger(M), 1, 2);
end;
我对动态数组的实现细节了解不够,TArray<T>
转换、引用计数等以确保这是安全的。
有没有人知道足够多的知识来以某种方式说出这是否会在运行时产生正确的代码?