The Hyper Programming Language |
home | language reference | compiler | hyper on launchpad |
Arrays are a basic instrument for programming. They are needed when using a set or collection of data and they will probably be used to implement the containers in the standard library.
In Hyper, arrays are not like in C++ and neither as in Java. An array is an object. It has a number of dimensions, and each of them has a size. You can choose to create an array on the fly with a size that is determined at runtime, as in Java. But you can use both the stack and the heap for this, while in Java only the heap can be used for arrays. Dimension sizes can be specified at compile time. Open arrays are also available.
Arrays are declared from left to right, as it is for all types. You write the dimensions between square brackets, followed by the base type. The base type is the type of the things you can store in the array. The type of an array with a single dimension is written like this:
[size] base-type
Multi-dimensional arrays are declared the same way. A two-dimensional array:
[size_0][size_1] base-type
The dimensions can be written apart from each other, like in the previous example, or they can be merged together and separated with commas:
[size_0, size_1] base-type
Merging dimensions is an alternative way to write the same thing, the two syntaxes are not semantically different.
Syntactically, the dimension sizes can be any valid expression. The size only needs to be a constant if you want to exchange pointers to arrays, or when passing an array as a parameter (unless you use an open array).
The base type of an array can never be another array. If you would declare a two-dimensional array with as base type an array with one dimension, then the two 'arrays' would be viewed together as one array with three dimensions.
Arrays cannot be declared constant. The size of an array cannot be increased or decreased, so in a way an array is always constant. If you don't want the elements of an array to be changeable, then you can make the array's base type constant.
When a variable or field is declared as an array, then every element of that array is initialized to its default value. Array initializers are not yet supported.
Most of the time, you will be using elements from the arrays in expressions. Selecting array elements to use them is written in the same way as the array was declared. For a two-dimensional array called 'a', you would use 'a[x][y]' to get or set the element with 'x' as the position for the first dimension, and 'y' as the position for the second dimension. You can use any expression instead of just 'x' or 'y' to access an element.
An array has one or more procedures that allow you to get its size. For an array with only a single dimension this is just one procedure called "size". Multi-dimensional arrays have one procedure for each of their dimensions, called "size0", "size1", "size2", etc.
An array instance cannot be treated as a value. In order to copy the contents of an array, you have to copy each element one by one. When an array is used as a parameter, it is passed by reference, but is unmodifiable by default. See parameter passing for more info.
A simple case of an array with 5 elements. We print the sum of the elements:
procedure p() var list : [5] int arr[0] = 152 arr[1] = -45 arr[2] = 6 arr[3] = -21 arr[4] = 99 system.out.printLn(sum5items(list)) end procedure sum5items(arr : [5] int) : int # array is passed by reference! var currentSum : int = 0 iterate i : nat from 0 count 5 currentSum += arr[i] end return currentSum end
The following is an example using a two-dimensional matrix. The second procedure in the example uses only the 'merged' notation for the arrays.
procedure test() var matrix : [2][2] single matrix[0][0] = 321.5 matrix[0][1] = -42.51 matrix[1][0] = 32422 matrix[1][1] = -512 system.out.printLn(determinant(m)) end procedure determinant(m : [2, 2] single) : single return m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1] end