leo/docs/error-guides/parser/array_tuple_dimensions_empty.md
2022-02-28 09:42:37 -08:00

931 B

Empty array dimensions

Example

This error occurs when specifying an empty tuple as the dimensions of an array.

Erroneous code example:

function main() {
    let foo = [42; ()];
}

The compiler will reject this code with, for example...:

Error [EPAR0370023]: Array dimensions specified as a tuple cannot be empty.
    --> test.leo:2:20
     |
   2 |     let foo = [42; ()];
     |                    ^^

Solution

If you wanted a single dimensional array, you can achieve that by specifying the length like so:

function main() {
    let foo = [42; 4];
}

This will give you the array [42, 42, 42, 42].

If instead you wanted a multi-dimensional array, e.g., a 2 x 3 matrix, you can achieve that with:

function main() {
    let foo = [42; (2, 3)];
}

Alternatively, you can use the simple syntax all the way instead:

function main() {
    let foo = [[42; 2]; 3];
}