How to pass generic array as open array parameter in Delphi?

delphi pass array as parameter
delphi array of const
delphi dynamic array
delphi pointer to array
delphi fill array
delphi array boolean
delphi array of records
delphi add to array

I have an enumerated type and I need to pass an array of this type as parameter:

  TTest = (a,b,c);

procedure DoTest(stest: TArray<TTest>);

When I compile


I receiv the error below:

Error: E2010 Incompatible types: 'System.TArray' and 'Set'*

So, how can I call DoTest without creating a variable of type TArray<TTest>?

One way to do what you want is to change the parameter to an open array of TTest, i.e.

procedure DoTest(const stest: array of TTest);

But supposed you don't want to change the parameter, and really want it to be a TArray<TTest>, then you can simply use the array pseudo-constructor syntax to call it (in almost all versions of Delphi, except the very old ones). Say you have something like:

  TTest = (a, b, c);

procedure DoTest(const stest: TArray<TTest>);
// simple demo implementation
  I: Integer;
  for I := Low(stest) to High(stest) do
    Write(Integer(stest[I]), ' ');

Then it can be called, using the Create syntax without having to declare a variable or having to fill it manually. The compiler will do this for you:

  DoTest(TArray<TTest>.Create(a, c, b, a, c));

The output is, as expected:

0 2 1 0 2

Open array parameters and array of const, then you are not passing a dynamic array constant, you are using an open array constructor. TArray<T>. In versions of Delphi that know generics, you will see the​  Open array parameters and array of const. This article describes the syntax and use of open array parameters, and the use of the “array of const” parameter type. It also describes the internals of these two similar types of parameters, discusses lifetime issues, and gives a code solution for these issues.

I don't have a Delphi compiler available right now, so I cannot verify this, but to me

procedure DoTest(stest: TArray<TTest>);

doesn't declare stest as an open array parameter, but a dynamic array parameter. You do want

procedure DoTest(const stest: array of TTest);

Declaring Generics - RAD Studio, In Delphi, you can use any type as a type argument except for the following: a TFoo<T>; // declared with open constructed type FFooBar1: TFoo<Integer>.TBar Class, interface, record, and array types can be declared with type parameters. Any array type can be passed to an open array parameter. In the second example, you are declaring a dynamic array of strings, but you are not allocating any memory for the array, and you are trying to assign its first element (which is a single string) to point at a fixed array of strings. And then you are trying to pass that element (again, a

The compiler may confuse a with another declaration.

Qualify the type like this:



This feature of initializing dynamic arrays was introduced in XE7.

Parameters (Delphi) - RAD Studio, Be careful not to overflow the stack by passing large arrays. The following examples use open array parameters to define a  The Args parameter is a dynamic array. Create and populate a dynamic array and pass that. Your code fails to compile because you attempt to pass an open array constructor. That would be valid if you had an open array, but you don't. You have a dynamic array.

I'm assuming that with "how can I call DoTest without creating a variable of type TArray" you want to avoid declaring and initializing local variable, ie code like

var arr: TArray<TTest>;
  SetLength(arr, 1);
  arr[0] := a;

For this you can use the array constructor like:


This syntaxs is supported at least since Delphi 2010.

Delphi array parameter, Nov 21, 2019 · Like in Delphi, you can pass parameters to DelphiScript routines by A variant open array lets you pass a heterogeneous array, that is, an array A method can also have a generic type parameter list and those parameter lists  When you pass an array as an open array value parameter, the compiler creates a local copy of the array within the routine's stack frame. Be careful not to overflow the stack by passing large arrays. The following examples use open array parameters to define a Clear procedure that assigns zero to each element in an array of reals and a Sum

Generic array slice, Well, wiki says that it grabs a slice of an array and puts it into another array, cool, Pretty cool, huh? in case you're wondering, TArraySlice is a generic record, Random Thoughts on the Passing Scene #162 - What is the Delphi editor tabs - When you've got many files open in the code editor of Delphi,  @dan-gph. When you define a type for an array, you have an Dynamic Array. Dynamic array is always pass by reference. In the other hand, a method that has a parameter like "array of X" is expecting an "Open Array", the rules for this open arrays are different, see here – EProgrammerNotFound Oct 8 '15 at 5:09

1. Delphi Pascal - Delphi in a Nutshell [Book], Like any Pascal language, Delphi supports enumerations, sets, arrays, integer Type variant open arrays give you a way to pass a variable size argument list to a The type Pointer is a generic pointer type, equivalent to void* in C or C++. The same sql invokes a simple "No Value for parameter "Test_id". With Delphi 2010 I get the following message "Der dbExpress-Treiber unterstützt den Datentyp TD Help converting Delphi code using dynamic array I am converting some Delphi code into C++Builder XE4.

Dynamic Array as optional parameter in Delphi 7 - arrays - php, Is it possible to pass to a function or procedure a dynamic array as optional See Is a dynamic array of Char allowed when the parameter type is open array of Char? I have arrived at a very satisfactory solution shown below using a generic  In Delphi, an open array is a built-in type and can be passed by value. In C++, the open array type is implemented using a pointer, which will modify the original array unless you make a local copy of it. Delphi functions that return arrays Delphi allows functions to return arrays, but C++ does not.