为什么 TArray<recordType> 不同于 recordType 数组?
Why is TArray<recordType> different from array of recordType?
我的记录类型定义如下:
type
TRecordType = record
Field1: string;
Field2: Variant;
end;
以及使用它的函数声明:
function Function1(const Records: TArray<TRecordType>): TAnyOtherClass;
到目前为止一切顺利,但如果函数调用如下:
Function1([BuildRecord('string', value), BuildRecord('OtherString', otherValue)])
编译器returns出错:
[DCC Error] AnyUnit.pas(142): E2001 Ordinal type required
很久以前我在某个地方读到 Delphi 的编译器在一种预处理器中处理泛型,在真正编译代码之前完成字符串替换,所以我期待 Function1
变成这样:
function Function1(const Records: array of TRecordType): TAnyOtherClass;
因为TArray
被定义为TArray<T> = array of T;
.
我认为这不会发生,因为当我将函数声明更改为:
function Function1(const Records: array of TRecordType): TAnyOtherClass;
代码编译无错误或警告。
[这个问题] 1 有一篇文章的答案 link 解释了差异,但是 link 已经损坏了。
所以我的问题是,如果不是 array of T
,TArray<T>
是什么意思?
Function1([BuildRecord('string', value), BuildRecord('OtherString', otherValue)])
你的论点中的 [...]
语法就是所谓的 open array constructor。该文档指出,我强调:
Open array constructors allow you to construct arrays directly within function and procedure calls. They can be passed only as open array parameters or variant open array parameters.
您的函数接受(通用)动态数组类型,这与 open array 不同。您的函数声明为
function Function1(const Records: TArray<TRecordType>): TAnyOtherClass;
该参数是一个(通用的)动态数组类型。开放数组参数如下所示:
function Function1(const Records: array of TRecordType): TAnyOtherClass;
我知道这看起来很像动态数组的声明,但事实并非如此。在Delphi中,array of
有两个不同的含义:
- 在参数列表的上下文中,
array of
用于声明一个开放数组参数。
- 在其他地方,
array of
定义了一个动态数组类型。
语言语法的这种重载是造成混淆的常见原因。
因此,由于这一切,编译器拒绝了您的代码,因为您正试图使用一个带有非开放数组参数的开放数组构造函数。
我在这里的回答更详细地讨论了这个问题:
I have read some place a long time ago that Delphi's compiler handles generics in a kind of preprocessor and some kind of string replace in code.
我觉得你记错了。您所描述的更类似于 C++ 模板。 Delphi 泛型不由预处理器处理,尤其是因为 Delphi 没有预处理器。
我的记录类型定义如下:
type
TRecordType = record
Field1: string;
Field2: Variant;
end;
以及使用它的函数声明:
function Function1(const Records: TArray<TRecordType>): TAnyOtherClass;
到目前为止一切顺利,但如果函数调用如下:
Function1([BuildRecord('string', value), BuildRecord('OtherString', otherValue)])
编译器returns出错:
[DCC Error] AnyUnit.pas(142): E2001 Ordinal type required
很久以前我在某个地方读到 Delphi 的编译器在一种预处理器中处理泛型,在真正编译代码之前完成字符串替换,所以我期待 Function1
变成这样:
function Function1(const Records: array of TRecordType): TAnyOtherClass;
因为TArray
被定义为TArray<T> = array of T;
.
我认为这不会发生,因为当我将函数声明更改为:
function Function1(const Records: array of TRecordType): TAnyOtherClass;
代码编译无错误或警告。
[这个问题] 1 有一篇文章的答案 link 解释了差异,但是 link 已经损坏了。
所以我的问题是,如果不是 array of T
,TArray<T>
是什么意思?
Function1([BuildRecord('string', value), BuildRecord('OtherString', otherValue)])
你的论点中的 [...]
语法就是所谓的 open array constructor。该文档指出,我强调:
Open array constructors allow you to construct arrays directly within function and procedure calls. They can be passed only as open array parameters or variant open array parameters.
您的函数接受(通用)动态数组类型,这与 open array 不同。您的函数声明为
function Function1(const Records: TArray<TRecordType>): TAnyOtherClass;
该参数是一个(通用的)动态数组类型。开放数组参数如下所示:
function Function1(const Records: array of TRecordType): TAnyOtherClass;
我知道这看起来很像动态数组的声明,但事实并非如此。在Delphi中,array of
有两个不同的含义:
- 在参数列表的上下文中,
array of
用于声明一个开放数组参数。 - 在其他地方,
array of
定义了一个动态数组类型。
语言语法的这种重载是造成混淆的常见原因。
因此,由于这一切,编译器拒绝了您的代码,因为您正试图使用一个带有非开放数组参数的开放数组构造函数。
我在这里的回答更详细地讨论了这个问题:
I have read some place a long time ago that Delphi's compiler handles generics in a kind of preprocessor and some kind of string replace in code.
我觉得你记错了。您所描述的更类似于 C++ 模板。 Delphi 泛型不由预处理器处理,尤其是因为 Delphi 没有预处理器。