Statements: array

Declares a local array. Local arrays and variables allow you to store intermediate results during the evaluation of expressions.

Syntax

arraytype‑expressionarray‑name[array‑size];

Discussion

The declaration determines the name of the new array, the data type of values the array can store at the runtime and the size (the capacity expressed in the maximum number of elements) of the array. Just after declaring, the local arrays are considered as not yet initialized - the values of all array elements are for the moment undefined. As such, evaluating an array element before you have assigned a value to it will report a Chora compiler error. For example:

array string text1[3]; array string text2[3]; // Initialize the array text1[0] = "Hello"; text1[1] = "Bonjour"; text1[2] = "Hola"; // Read the value of the array var string result = text1[2]; // result = "Hola" // Modify the Variable text1[2] = text1[2] + " mundo"; // text1[2] = "Hola mundo" // Try to read a non-initialized array var string result = text2[2]; // Chora error

The actual size of an array results from the array-size operand. In its simplest form, the operand consists of a single unsigned integer literal. Thus declared arrays are considered as one-dimensional. In turn, multidimensional arrays are declared with the array-size operand containing several unsigned integer literals (one for every array dimension) separated by the , comma sign. To evaluate the actual size of an already declared array use the size instant property. For example:

array string text1[ 16 ]; // One-dimensional array array string text2[ 2, 3 ]; // Multidimensional array with 2 x 3 = 6 elements var int32 i; // Initialize all elements of the one-dimensional array for ( i = 0; i < text1.size; i = i + 1 ) text1[ i ] = ""; // Initialize a multidimensional array text2[0,0] = "Hello"; text2[0,1] = "Bonjour"; text2[0,2] = "Hola"; text2[1,0] = " world"; text2[1,1] = " monde"; text2[1,2] = " mundo"; // Read the value of the array var string result = text2[0,1] + text2[1,1]; // result = "Bonjour monde"

Please note, Chora expects the size of an array to be well known at the compilation time and immutable at the runtime. Using variable expressions in the array-size operand is thus not allowed:

var int32 a = 10; array string text3[ a ]; // The size is not a constant value. Chora error

The array statement can occur everywhere within the method or within a nested block statement wherever regular statements are allowed. The blocks serve as individual scopes for all enclosed local arrays and variables. In other words, the arrays are valid within the respective block only. Outside the block, these definitions are not available and their contents are lost. For example:

if ( operand1 ) { // Following arrays are valid within this block only array rect arr1[2]; array string arr2[2]; arr1[0] = SomeTextView1.Bounds; arr1[1] = SomeTextView2.Bounds; arr2[0] = SomeTextView1.String; arr2[1] = SomeTextView2.String; [ ... ] } array rect arr1[2]; array string arr2[2]; arr1[0] = OtherTextView1.Bounds; arr1[1] = OtherTextView2.Bounds; arr2[0] = OtherTextView1.String; arr2[1] = OtherTextView2.String; [ ... ]