当Char的参数类型是开放数组时,是否允许Char的动态数组? [英] Is a dynamic array of Char allowed when the parameter type is open array of Char?
问题描述
我正在查看 http://stackoverflow.com/q/3780235/71200 并开始实验。我发现是非常有趣的。
I was looking at http://stackoverflow.com/q/3780235/71200 and started experimenting. What I discovered is rather interesting.
procedure Clear(AArray: array of Integer);
var
I: Integer;
begin
for I := Low(AArray) to High(AArray) do
AArray[I] := 0;
end;
var
MyArray: array of Integer;
begin
Clear(MyArray);
end.
这个简单的小例子显示了如何使用Open Array参数将动态数组传递给一个过程。它按照预期的方式编译和运行。
This simple little example shows how you can pass a Dynamic Array to a procedure using an Open Array parameter. It compiles and runs exactly as expected.
procedure Clear(AArray: array of Char);
var
I: Integer;
begin
for I := Low(AArray) to High(AArray) do
AArray[I] := #0;
end;
var
MyArray: array of Char;
begin
Clear(MyArray);
end.
这是几乎相同的代码,唯一的区别是使用 Char
而不是整数
。它不编译相反,编译器会吐出来:
Here is nearly identical code the only difference being it is using an array of Char
rather than Integer
. It does not compile. Instead the compiler spits out:
E2010 Incompatible types: 'Array' and 'Dynamic array'
为什么会这样?
搜索一段时间后,我发现此 QC报告。我正在运行Delphi 2009,它仍在发生。
After searching for a while I discovered this QC report. I'm running Delphi 2009 and its still happening.
推荐答案
由于文档特别提到了Char类型的开放数组参数,使用动态数组,这应该是一个错误。从打开阵列参数:
Since the documentation specifically mentions open array parameters of type Char to be compatible with dynamic arrays, this should be a bug. From 'Open Array Parameters':
function Find(A:array of Char):
整数;
注意: [...]
以前的示例创建一个函数
,它使用任何Char元素数组,
包括(但不限于)动态
数组。 [...]
function Find(A: array of Char):
Integer;
[...]
Note: [...] The previous example creates a function that takes any array of Char elements, including (but not limited to) dynamic arrays. [...]
这篇关于当Char的参数类型是开放数组时,是否允许Char的动态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!