2

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.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • 2
    try sum_of_even_squares( ([7])[1..0] ) == 0) which makes an empty array by slicing an array literal. The inner parentheses seem to be required right now but I'm not sure if that's a bug or not. – mppf Apr 06 '17 at 13:44

1 Answers1

4

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.

Cimbali
  • 11,012
  • 1
  • 39
  • 68
mppf
  • 1,820
  • 9
  • 9
  • 1
    Yes, this makes sense. Good point about empty array literals not being able to infer a type (without some kind of annotation). – Ray Toal Apr 03 '17 at 22:28