I want arrays in silent-lang to be as intuitive as possible, this is what happened to my design.
Array creation
1 | let n = input |
Uninited array begins with random values, they should be treated as non-accessible.
The rep
keyword (stands for “repeat”) is for avoiding ambiguity between value multiplication, while keeping a semantically-clear syntax.
Array indexing
Same as C, just:
1 | let arr = [2, 3, 4] |
0
means the first element.
Array modification
1 | let n = input |
Please note that an instance of array is affine, meaning that:
- After being referenced as a whole (not indexing), it can no longer be used
- Being referenced is not mandatory
For example, this is illegal:
1 | let n = input |
Reason: after calculating arr[x <- arr[y]]
, arr
is no longer referenceable. Please always remember that let
is associating a name with contents of specific memory address, not manipulating memory directly, which is the duty of referencing.
Array length
I would probably use a struct to represent an array:
1 | struct array { |
This way, you can refer to the length (capacity) of an array with:
1 | let arr = [1, 2, 3] |
Just like Golang.
Array typing
I’d like to add dependently-typed arrays afterwards, but for now I’ll just make a simple one:
1 | let f = (1 arr: int[]) => arr[0 <- 5] |
Just like C.
Not supported
Array concatenation
Concatenation violates the philosophy of silent-lang: view mutations as silent functions from state to state, not how to dynamically construct the result.
Dynamic-lengthed Array
Once finishing structs in silent-lang, dynamic-lengthed array can be implemented inside silent-lang. I want the core language to contain as few features as possible.