如何在 Delphi 中初始化通用记录的常量数组
How to initialize constant array of generic record in Delphi
在 Delphi 中,我可以像这样初始化非通用记录的常量数组:
type
TMapEntry = record
Key: Integer;
Value: Integer;
end;
const
Arr: array[0..0] of TMapEntry = ((Key: 1; Value: 10));
但是当我尝试以更通用的方式扩展代码时,我无法初始化常量数组:
type
TMapEntry<KeyType, ValueType> = record
Key: KeyType;
Value: ValueType;
end;
const
Arr: array[0..0] of TMapEntry<Integer, Integer> = ((Key: 1; Value: 10)); //<-- Compile error
我什至尝试使用类型别名,但遇到了同样的编译错误:
type
TIntMapEntry = TMapEntry<Integer, Integer>;
const
Arr: array[0..0] of TIntMapEntry = ((Key: 1; Value: 10)); //<-- Compile error
有什么方法可以初始化通用记录的常量数组吗?
PS: 我正在使用 Delphi 10.3
Is there any way to initialize a constant array of generic record?
没有。不幸的是,这是编译器的限制。
在 Delphi 中,我可以像这样初始化非通用记录的常量数组:
type
TMapEntry = record
Key: Integer;
Value: Integer;
end;
const
Arr: array[0..0] of TMapEntry = ((Key: 1; Value: 10));
但是当我尝试以更通用的方式扩展代码时,我无法初始化常量数组:
type
TMapEntry<KeyType, ValueType> = record
Key: KeyType;
Value: ValueType;
end;
const
Arr: array[0..0] of TMapEntry<Integer, Integer> = ((Key: 1; Value: 10)); //<-- Compile error
我什至尝试使用类型别名,但遇到了同样的编译错误:
type
TIntMapEntry = TMapEntry<Integer, Integer>;
const
Arr: array[0..0] of TIntMapEntry = ((Key: 1; Value: 10)); //<-- Compile error
有什么方法可以初始化通用记录的常量数组吗?
PS: 我正在使用 Delphi 10.3
Is there any way to initialize a constant array of generic record?
没有。不幸的是,这是编译器的限制。