Experimentation Stage

Arrays

In some other languages arrays are normally primitive types or at least fundamental types. In Lense, arrays are simply objects of the Array class. This class belongs to the Collections API and receives no special handling by the language or the compiler. The Collections API is a set of common object structures like hash tables and linked lists implemented in a coherent framework.

You create arrays as you would create any other class, by invoking one of its constructors. There is no new SomeArray[2] syntax for array definition. Arrays in Lense are the default implementation of the EditableSequence interface that behaves like arrays in other languages. `EditableSequence`s have fixed length and values can be set at given positions, but new objects cannot be added to the sequence. However , because Lense has no `null`s, arrays must always be pre-initialized.

Initialization

Because Lense does not have the concept of null arrays must be initialized correctly with a specified value. There are no default values. If you cannot determine the initialization values you will be better using a List( or any other ResizableSequence like LinkedList) that allows you to start with zero elements in the collection and add the elements as you go. When you use Array you need to supply the elements at creation time. The easiest way to do this is with a sequence literal:

	// with type annotation
    let numbers : Array<Natural> = [1, 2, 3 , 4 , 5];
	// with no type annotation, using type inference
    let numbers = [1, 2, 3 , 4 , 5];

Remember Array is not a fundamental type in Lense. That literal above really creates a Sequence object that is read-only by definition. The compiler then uses a constructors.html#conversion[conversion constructor] to create the Array from the Sequence. If you wish to initialize all position in the array to the same value you can use a special constructors.html#named[named constructor]. The next example crates array with 5 elements all equal to zero.

    // with type annotation
    let numbers : Array<Natural>  = new Array.filled(5, 0);
	// with no type annotation, using type inference
	let inferedTypeNumbers = new Array.filled(5, 0);

If a constant is not a good option in your case, you can, alternatively, use the overloaded version that receives a initialization function. This code also creates an array with 5 elements, but in this case they correspond to the first 5 even numbers.

    // this code creates an array with 5 elements corresponding to the first 5 even numbers.
    let numbers = new Array.filled(5, i -> 2 * i);

This constructor receives a lambda expression to initialize each elements from the position in the array represented by the i parameter (can be any variable name). This parameter assumes all values for the position in the array from 0 to the size array exclusively (4 in this case). In the example we initialize the arrays with the first 5 even numbers.

Optional values

Alternatively you can create an array with an optional type. This means each position of the array can have an absent value. You can create an array of an optional type the same way you create it for any other type. The following also created an array with 5 positions, but this time they hold the none value

    // with type annotation
    let numbers : Array<Natural?> = new Array.filled(5, none);
	// with no type annotation, using type inference
	let inferedTypeNumbers = new Array.filled(5, none);

Remember none is the single value of type None that is equivalent to Maybe<Nothing>. You can also use the shorter constructor:

	let numbers = new Array.ofAbsent<Natural>(5);

Empty Arrays

In practice is useful to be able to create an empty array. The empty constructor solves this problem creating an array with no elements at all.

    // creates an array of non-optional elements with no elements in it
    let numbers :  Array<Natural> = new Array.empty<Natural>();

    // creates an array of optional elements with no elements in it
    let optionalNumbers : Array<Natural?> = new Array.empty<Natural?>();

In this case there is no difference if you use optional types because the arrays have no elements (its size is zero) so not elements can be retrieved any way

Indexing

Arrays are specially useful because the values at each position can be access by a second variable : the index. Normally, in other languages, this index is an integer. In java, for example, you would write:

    int[] array = new int[]{1,2,3};

	int x = array[1]; // x is 2

	array[2] = 6; // array position 2 is now 6 instead of 3.

In java, and other languages, the array is a fundamental type and so the language and the compiler have special treatment for the index operator []. In Lense arrays are not fundamental, but the index operator is. In reality is not an operator, is the way you use indexed properties. Indexer properties are members of types that allow to read and write values based on one or more indexes. In Lense all Sequence`s have an indexed property for reading values at a given position in the sequence, and for `Array`s is also possible to write to that property since it is a `EditableSequence.

In practice you can still use the array[i] syntax to read from, and write to, array positions , like in other languages:

public updateArray( numbers : Array<Natural> ) {
    numbers[0] = 1;
    numbers[1] = 2;
    numbers[2] = 3;
    numbers[3] = 2 * numbers[1];
    numbers[4] = numbers[1] + numbers[2];
}

But you can also using indexes with other classes.

Additionally, indexes for all Sequences are `Natural`s. Natural ranges from zero onwards in the positive direction, so no "negative index" validation is necessary.

Interoperability

Array s and other Sequence s have some issues in interop mode because they counterparts in other languages can be null and contain `null`s.

On the other hand, when creating instances of arrays, Lense will leverage native arrays to minimize space consumption and optimize speed. This is done by leveraging Lense’s factory like constructors and reified generics to provide the more specific/efficient implementation possible. Obviously this is only possible in some platforms.