arrays

Arrays and literal arrays

Array is a generic prototype that cannot be inherited for sake of efficiency. It has methods that mirror those of class ArrayList of Java. Arrays grow as necessary to accommodate new elements, which are always inserted at the end unless the new element replaces an existing one. For example:

var Array<Int> v = Array<Int>(3);
v add: 10; // position 0 has element 10, others are empty // runtime error! No element at position 2 var Int k = v[2]; v = Array<Int>(); v add: 10; v add: 20; v add: 30; assert v[2] == 30 && (v at: 2) == 30;

Arrays supports some interesting methods: apply:, .*, .+, and foreach:. The first two ones apply an operation given as string to all array elements. Method .+ applies the operation passed as parameter (as a string) to all array elements and returns the result ( (v[0] op v[1]) op v[2], for example). It is assumed that the type of the array elements supports the binary operation passed as a string.

The language supports literal arrays:

var Array<Int> v = [ 2, 3, 5, 7, 11 ];

var letters = [ ’b’, ’a’, ’e’, ’i’, ’o’, ’u’, ’c’, ’d’ ];

v apply: #print; // print all array elements

All elements should have the same type.