- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的记录类型定义如下:
type
TRecordType = record
Field1: string;
Field2: Variant;
end;
以及使用它的函数声明:
function Function1(const Records: TArray<TRecordType>): TAnyOtherClass;
到目前为止一切顺利,但如果该函数的调用方式如下:
Function1([BuildRecord('string', value), BuildRecord('OtherString', otherValue)])
编译器返回错误:
[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该链接指向一篇解释差异的文章,但该链接已损坏。
所以我的问题是,TArray<T>
是什么意思?如果没有array of 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
定义动态数组类型。这种语言语法的重载是造成困惑的常见原因。
因此,由于所有这些,编译器会拒绝您的代码,因为您尝试使用带有非开放数组参数的开放数组构造函数。
我在这里的回答更详细地讨论了这个问题:https://stackoverflow.com/a/14383278/505088
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 没有预处理器。
关于arrays - 为什么 TArray<recordType> 与 recordType 数组不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32376610/
我的记录类型定义如下: type TRecordType = record Field1: string; Field2: Variant; end;
我必须存储一个可以在 Delphi 中轻松实现为记录的 TList(五个简单字段)。但是,我不清楚当我这样做时会发生什么 TList.Add(R) 。 由于 R 是我创建 TList 的过程中的局部变
我是一名初学者,到目前为止已经弄清楚如何以编程方式在 CloudKit 中创建 RecordType。 我在网上搜索过,几乎尝试了所有方法,但它们不起作用,很可能依赖于我还不理解的另一个变量。 这是我
我无法让 aws route53 服务的 ListResourceRecordSets 按 StartRecord 类型进行过滤。即使使用 StartRecordType 过滤器,它也会返回所有记录(
我有一个对象,其选项对应于以下记录类型: const AwsRegionsEnum = $.EnumType( 'AWS/Regions', 'http://docs.aws.amazon.c
我在 CloudKit 上工作,但不断收到字符不允许错误。允许使用哪些字符? 最佳答案 在 CKRecord.init(recordType:) 的文档中它说: Record type names m
我是一名优秀的程序员,十分优秀!