Next: Array Comparison, Previous: Casting Arrays, Up: Arrays [Contents][Index]
Array literals can only specify arrays which a constant number of elements. They also require initial values for their elements, and it is also not possible to construct an array literal denoting an empty array of some given type.
Array constructors provide a suitable way to build such array values. The syntax is:
array_type ([value])
Where array_type is an array type, or the name of an array type, and value is an optional argument that specify the value to which the elements of the new array are initialized. If no value is provided in the constructor then default values are calculated.
If the boundaries of the array can’t be satisfied while constructing,
E_constraint
is raised.
Examples:
(poke) int[]() [] (poke) int[2]() [0,0] (poke) int[2](10) [10,10] (poke) offset<int,B>[2](16#b) [2#B,2#B] (poke) string[3]() ["","",""] (poke) int[][3](int[2](666)) [[666,666],[666,666],[666,666]] (poke) Packet[3]() [Packet {...}, Packet {...}, Packet {...}]
Note that array constructors are implicitly involved when you construct a struct or union value that contains a field whose type is an array. For example, consider the following type:
type Packet = struct { byte sz; byte[sz] data; };
When a packet is constructed using Packet {}
the data
field of the struct is also constructed, implicitly using a
constructor for the array type byte[sz]
.