Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Jun 30, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@ghost
Copy link

ghost commented Jun 30, 2024

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]
@ghost ghost added the enhancement New feature or request label Jun 30, 2024
@goldenstein64
Copy link

goldenstein64 commented Jul 1, 2024

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.

@ghost
Copy link
Author

ghost commented Jul 1, 2024

Oh, thank you I forgot about that!

@ghost ghost closed this as completed Jul 1, 2024
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant