Search code examples
delphidynamic-arraysopen-array-parameters

Is a dynamic array of Char allowed when the parameter type is open array of Char?


I was looking at Delphi: array of Char and TCharArray "Incompatible Types" 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.

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.

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'

Why would this be?

After searching for a while I discovered this QC report. I'm running Delphi 2009 and its still happening.


Solution

  • 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): Integer;
    [...]
    Note: [...] The previous example creates a function that takes any array of Char elements, including (but not limited to) dynamic arrays. [...]