Arrays
The language currently implements Arrays, which are 1D only, with multi-dimensional arrays planned for future versions. The following points define the semantics of arrays and their conventions:
- The length and the type of the array has to be specified at declaration
- Initializing the array at declaration is optional, as all the elements will by default be initialized to zero
- The whole array in a batch can be initialized only while declaration, otherwise it has to be updated by its individual elements.
- Base index of the array is 0
- Arrays can be passed as arguments to functions and returned as results from functions.
- Arrays are allocated on the heap and are automatically managed by the garbage collector
Declaration
fn getSpecialConstant() int {
return 10 * 20 + 40;
}
#Declaring and initializing the array
int[10] a = [10,20,30,40,50,60,70,80,90,getSpecialConstant()];
fn start() void {
# Only Declaring an array
double[10] a;
return;
}
Accessing an Element
#Declaring and initializing the array
int[10] a = [10,20,30,40,50,60,70,80,90,100];
#Declaring an array
double[5] b;
fn start() void {
int i = a[1] + 10; # 30
double j = b[1] + 10.0; # 10.0
return;
}
Updating an Element
#Declaring an array
double[5] b;
fn start() void {
double j = 9.999;
b[4] = j * 10.11; # Updating the 5th element
return;
}
Passing an Array
consume fn printdouble(double val) void;
fn changeValue(double[5] t) void {
t[1] = 10000.1;
return;
}
fn start() void {
double[5] b;
double j = 9.999;
changeValue(b);
printdouble(b[1]);
b[4] = j * 10.11; # Updating the 5th element
return;
}
Returning an Array
consume fn printdouble(double val) void;
fn generateArray() double[5] {
double[5] a;
return a;
}
fn start() void {
double j = 9.999;
double[5] b = generateArray();
b[4] = j * 10.11; # Updating the 5th element
printdouble(b[4]);
return;
}