Destructuring syntax for arrays to variables

As a Roblox developer, it is currently too hard to quickly and cleanly assign variables from an array.
For example, lets say I have an array:
local array = {'a', 'b', 'c', 'd', 'e'}

If I wanted to create variables assigned to each of the elements in the array I would have to do the following:

local a1, a2, a3, a4, a5 = array[1], array[2], array[3], array[4], array[5].

This is very long and verbose. In many other languages, there is a much shorter and concise syntax to represent the same thing using destructuring:

local [a1, a2, a3, a4, a5] = array

This could also be applied to functions as well, for example, if a function expects an argument of an array with size of 4 you could do:

local function([a1, a2, a3, a4]).

This is obviously a far superior way to assign variables from an array, as it takes up half the space while conveying the same meaning while not being anywhere near as verbose as what we have to do currently.

If Roblox is able to address this issue, it would improve my development experience because it would make much easier to write shorter assignment operations in my code, making development faster, and also making code easier to read.

2 Likes

Sorry if I’ve misunderstood, but does unpack do what you’re looking for?

local tbl = {'a', 'b', 'c', 'd', 'e'}

local a1, a2, a3, a4, a5 = unpack(tbl)

print(a1, a2, a3, a4, a5) --> a b c d e
2 Likes

There’s an issue on the Luau GitHub repository regarding this feature request, there are a lot of things to consider when proposing features, especially ones like these that can increase syntax complexity.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.