3

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

type
  TTest = (a,b,c);


procedure DoTest(stest: TArray<TTest>);

When I compile

DoTest([a]);

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>?

10
  • 1
    TArray<Ttest> is not an open array parameter - array of Ttest would be. Commented Jun 22, 2018 at 19:30
  • @StefanGlienke, yes that is clearly a misunderstanding of how such a parameter is called. Commented Jun 22, 2018 at 19:35
  • Your question is quite odd. Think about it. "I have a function that accepts TArray<TTest>. How can I call it without passing a TArray<TTest>?" Surely it's obvious that you cannot do that. Change the function to accept an open array parameter. Commented Jun 22, 2018 at 20:04
  • 1
    Hard to reconcile that claim not to know what open array parameter is given the title of this question. You just typed those words completely by accident. Seriously, read documentation. Don't give up. It's so much easier if you read documentation. Commented Jun 22, 2018 at 20:26
  • 1
    When you say he you mean I or are you sharing an account? Commented Jun 22, 2018 at 20:47

4 Answers 4

4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

@LURD: Without a Delphi compiler available, I cannot answer that. (Also, it might have changed in XE7.)
Shameless plug: Open array parameters and array of const, written by me.
3

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:

type
  TTest = (a, b, c);

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

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:

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

The output is, as expected:

0 2 1 0 2

Comments

1

The compiler may confuse a with another declaration.

Qualify the type like this:

DoTest([Ttest.a]);

Note:

This feature of initializing dynamic arrays was introduced in XE7.

10 Comments

I tested this with Delphi 2010 and it gives the same error OP posts (compiler thinks it is a set, not array).
@ain, this was a feature introduced in XE7.
OK, the OP should specify his Delphi version and what he means with "without var" (ie is my solution "creating an var" or not)
"How can I call DoTest without creating a variable of type TArray<TTest>?"
@DavidHeffernan, how do you create a variable of type TArray<TTest>?. My answer works well in XE7+ and initializes a dynamic array.
|
1

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>;
begin
  SetLength(arr, 1);
  arr[0] := a;
  DoTest(arr);

For this you can use the array constructor like:

DoTest(TArray<TTest>.Create(a));

This syntaxs is supported at least since Delphi 2010.

8 Comments

how call DoTest wihout create a var of array of TTest
Hmm, I think the OP means without a local variable... creating an array "inline" is not the same?
Either way, this produces the same code as my answer if the compiler supports the XE7 introduced way of initializing dynamic arrays.
"How can I call DoTest without creating a variable of type TArray<TTest>?"
@ain Difficult to know what is meant when the terminology is imprecise. You declare variables and create objects. You don't create variables.
|

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.