Ray Toal
Ray Toal

Reputation: 88428

Is there a literal for the empty array in Chapel?

I'm trying to make an empty array in Chapel. An array of one element can be made like this:

var a: [1..1] int = (1);

But when I try

var b: [1..0] int = ();

I get

syntax error: near ')'

Is there an empty array literal in Chapel? I haven't been able to find an example.

EDIT

The reason I am trying to get an empty array is that I would like to implement get this function working for empty arrays:

proc sum_of_even_squares(a) {
  // Does this work for empty arrays? Probably not.
  return + reduce ([x in a] if x % 2 == 0 then x*x else 0);
}

assert(sum_of_even_squares([7]) == 0);
assert(sum_of_even_squares([7, 3]) == 0);
assert(sum_of_even_squares([7, 3, -8]) == 64);
assert(sum_of_even_squares([7, 3, -8, 4]) == 80);

But I am unable to form an empty array literal.

Upvotes: 2

Views: 262

Answers (1)

mppf
mppf

Reputation: 1845

Generally, in Chapel, to declare an empty thing, you specify its type but no initialization, such as

var i:int;

But to declare an integer initialized with a value, you'd probably leave off the type:

var j = 2;

In this case, simply omitting the initializer makes it work.

var b: [1..0] int;

Relatedly, (1) is not declaring an array literal but rather a tuple literal. The syntax [1] would declare an array literal. At present, zero-length tuples are not supported in the compiler implementation. It might be easier to get zero-length array literals to work, but it doesn't seem to work right now either (in 1.15).

And how would a zero-length array literal know the type of the elements? For this reason I don't think it could help in your specific case.

Upvotes: 4

Related Questions