The content on this page is written in Chinese, and then traslated into English by machine. More accurate traslations are welcome at: https://github.com/wa-lang/man/tree/master/en
Ending's law: "Any application that can be compiled to WebAssembly, will be compiled to WebAssembly eventually."
5.2. Array
The basic declaration of an array type is as follows:
[N]T
Among them, N
is the array length (a constant integer greater than 0), and T
is the array element type; for example:
a: [3]i32
a[0] = 42
println(a[0]) // 42
Like many languages, Wa-lang use the x[M]
syntax to access specific elements within an array. When declaring an array variable, you can use the following methods to set the initial value of the array element:
a: [3]i32 = [3]i32{13, 42, 9527}
println(a[0], a[1], a[2]) // 13 42 9527
[3]i32{13, 42, 9527}
declares an array literal, so the declaration of the array variable a
in the above example can be simplified to:
a := [3]i32{13, 42, 9527}
When declaring an array literal, the number of elements contained in the subsequent {}
can be less than the array length (but not greater), and the missing part is a 0 value, for example:
a := [3]i32{13, 42}
println(a[2]) // 0
The built-in function len
can be used to get the length of an array, which is the number of elements contained in the array, for example:
a := [3]i32
println(len(a)) // 3
When declaring an array literal, if the array length part is written as ...
, it means that the array length is determined by the number of elements in the following {}
, for example:
a := [...]i32{13, 42}
println(len(a)) // 2
Arrays are value types in Wa-lang, for example:
a := [...]i32{13, 42}
b: [2]i32
b = a
println(b[0], b[1]) // 13 42
b[0] = 9527
println(a[0]) // 13
It can be seen that when assigning an array to another array, each corresponding element will be assigned, which means a deep copy operation is performed. Since arrays are values, there is no correlation between assigned arrays.
Arrays with the same element type but different lengths are considered different types, so the following program is illegal:
a: [2]i32
b: [3]i32
b = a // Illegal, different types cannot be assigned
Known issues:
- Bounds checking is not performed when accessing array elements via variable subscripts. This problem does not affect syntax compatibility. Subsequent corrections to this problem will not affect the existing source code. Developers using Wa-lang do not need to deal with this issue specially.
- In the current version of the implementation, the array is expanded into a set of linear values. Therefore, when the array is assigned, the number of virtual registers and instructions is an integral multiple of the array length. If the length is too large, the size of the target code will expand rapidly. This problem does not affect syntax compatibility, and subsequent corrections to this problem will not affect existing source code. At this stage, program developers should pay attention to: except for global variables, try not to use arrays with a length greater than 8.