luau-lang/luau

Unable to Table Assign and Locally Assign Variables at the Same Time While Unpacking

Closed this issue · 2 comments

You can

local a, b = 1, 2

However you cannot unpack something into a table and local variable at the same time. This code gives you a syntax error.

local myTable = {}
local myTable.a, b = 1, 2

Likewise, you cannot move the local keyword, or you will get a syntax error.

local myTable = {}
myTable.a, local b = 1, 2

It would be nice to be able to do the code above rather than manually unpacking it.

local myTable = {}
local toUnpack = {1, 2}
myTable.a = toUnpack[1]
local b = toUnpack[2]

It's possible to forward-declare a local variable first before assigning.

local toUnpack = { 1, 2 }
local myTable = {}
local b: number
myTable.a, b = unpack(toUnpack)

This should give you the same behavior.

Oh, thank you I forgot about that!