-
Notifications
You must be signed in to change notification settings - Fork 5
Arrays
SuperForth arrays are all linear and one dimension, although multidimensional arrays can still be achieved with arrays of arrays and so forth. Note: There is one important caveat about arrays: While primitives and even procedures are passed by value, arrays are passed by reference. To be more specific, their pointers are. That means that you can modified an argument-passed array's elements, but not the array itself.
Arrays and their elements are all stored on the heap (via dynamic allocation), while variables, constants, and generally, primitives are stored on the "stack". The following examples demonstrates the usage of the new
keyword to allocate a new array:
int n = 7;
array<int> a = new int[20];
auto b = new bool[n];
The keyword new
must be followed by a type specifier (read more about types here) and by a integer value specifying length encapsulated in brackets.
Note: Reading an uninitialized (or unset) value from an array will invoke the runtime error ERROR_READ_UNINIT
.
Array literals are quite akin to normal array allocations, however the size and elements are predetermined. It is impossible to incur an ERROR_READ_UNINIT
via array literals. Here are a few examples:
array<int> a = [1,2,3,4,5,6,2];
array<bool> b = [true, true, false];
String literals, data encapsulated in quotation marks, are ultimately array literals of characters. They get translated into array<char>
.
- Getting Started
- The Language and Syntax
- Type Declarations
- Primitive Values
- Collection/Object Values
- Operators
-
The Standard Library
- More docs coming soon after APs.
- FFI and Interoperability
- The Implementation