The Freya Programming Language

Array types

See also

Array types allow representing and managing simple series of items sharing the same type. In Freya, arrays are special reference types derived from the System.Array predefined class and a base type which determines the type of the array items.

Declaring array variables and fields

Array types can be used to declare fields, parameters and local variables, as in this example:

Stack = class
private
    Items: Array[Integer];
    // ...
end;

.NET arrays are first class objects, instead of mere collection of contiguous memory items. At least in theory, all array types are classes derived from the System.Array class. You can use all instance members defined by the Array class with an array instance.

Actually, the runtime tweaks a little bit the array implementation. For instance, we are told all array types implement these generic interfaces:

However, you won't discover this even if you use the Reflection API at runtime.

Array theory

All these problems are a consequence of the lack of generic types in the first .NET release. Freya attemps to provide a more uniform type system by pretending that array types are closed instances of the Array class, as if Array were a generic class (it is not).

Array types are constructed types: you cannot define an array type and give it a permanent name in the .NET CLR world. As a consequence, array types you declare are intended for immediate use. This shouldn't be a great issue: as a matter of fact, this is also the case with closed generic types, and with nullable types.

Multidimensional arrays

All array types we have shown so far are unidimensional arrays: you must provide an integer value to retrieve an array item. However, Freya, as most .NET languages, also supports multidimensional arrays, which require more than an index to retrieve a given item:

method GetTrace(A: Array@2[Double]);
// The trace of a matrix is the sum of its diagonal elements.
begin
    // We don't know how many rows and columns this array has.
    // It may even be a rectangular matrix!
    var upperLength := Math.Min(A.GetLength(0), A.GetLength(1));
    for I: Integer := 0 to upperLength - 1 do
        Result += A[I, I];
end;

Freya assumes the existence of a family of generic array classes with names as Array@2 and Array@3, for creating multidimensional array types. Of course, none of these classes exists.

See also

The Freya Programming Language
Constructed types
Class declarations
Records declarations
Generic types
Nullable types
Type declarations