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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It just looks like it works. But there are some actions that will fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.