Skip to content

Arrays

Arrays are quite simple.

An array represents a contiguous list of values in a table. I.e., { "a", "b", "c" }

An array shouldn't have any nils or "gaps". If you want holes in a table, use Maps.

You can define a valid array Datatype using a simple table, just like luau:

1
2
3
local ty = light.datatypes

local some_array = { ty.u8 }

Using the above table syntax will behave the same as passing the table into the API shown below.

function light.datatypes.arr

Shared Synchronous
1
2
3
4
function arr<Item>(
    item: Datatype<Item>,
    length: Datatype<number>?
): (Datatype<{T}>)

First argument should be any Datatype which cannot be nil. The length parameter describes the number of items in the array, and will default to datatypes.u16.

The length datatype should NOT be a regular number—instead: use a datatype that represents a number, like a uint, or a range.

A couple of ways you could use the optional length parameter:

local some_arr = ty.arr( ty.u8, ty.range(0, 50) ) -- Array should have between 0 and 50 items.
local some_arr = ty.arr( ty.u8, ty.literal(3) ) -- Array will always have three items.
local some_arr = ty.arr( ty.u8, ty.u8 ) -- between 0-255 items.