Arrays
Arrays map a fixed range of integer indices starting at 0 to a set of values of the same type. The run-time cost of accessing the value of an arbitrary index in an array is typically constant (disregarding caching and similar effects).
An array in fuzion is a generic feature array T
that is
part of the Fuzion standard library. Apart from some intrinsic functions for an
efficient implementation, arrays are features just like any other feature in
Fuzion. They can be used as types, as parents for inheritance, as generic
arguments, etc.
Array Initialization
Since Fuzion does require values to be initialized before they are used, arrays must provide a means to initialize their elements when they are created. Thus an Array receives a function argument that provides the initial values for its elements. This function can also be used to fill the array with data.
Array Initialization Function
Here is a small example that fills an array of i32
values with
the squares of the corresponding indices and then prints the mapping:
Direct Array Initialization
As an alternative to an initialization function, syntactic sugar allows the
direct initialization of an array from a list of expressions enclosed in
brackets [ ]
:
Array Mutability
Arrays in Fuzion are immutable. Which means you can not change its elements.
If you need to mutate the elements in an array you can use mutate.array T
shown in the following example:
If you now analyze this example for effects you will find that it uses the mutate effect among others.