2

Check the following code:

function A(const points: TArray<TPoint>): Boolean;
begin
   //Something
end;

procedure B(var pts: array of TPoint)
begin
   A(pts); //Compiler error E2010 Incompatible types
end;

It yields a compiler error:

E2010 Incompatible types: 'System.TArray' and 'array of TPoint'

Calling A(TArray<>(pts)); doesn't work. I solve the problem doing

A(TArray<TPoint>(@pts));

Is it the proper way to typecast an open array parameter to TArray<>? Is there any other way?

Please assume that parameters type of both function can not be changed.

Delmo
  • 2,188
  • 2
  • 20
  • 29
  • Possible duplicate of [Why casting an open array parameter to an array type causes E2089 Invalid typecast?](https://stackoverflow.com/questions/50414260/) – Remy Lebeau May 29 '18 at 19:37

1 Answers1

3

You cannot perform such a typecast. An open array is no a dynamic array. Your options include:

  1. Use a dynamic array for both procedures.
  2. Use an open array for both procedures.
  3. Copy the content of the open array to a dynamic array, and pass that.

Of these I would note that copying is expensive and I would reject that option on principle.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490