Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpack table into same-named variables

If I have a table with known properties, but where the order of the properties is potentially unknown:

local person = {
    name = 'James',
    age = 30,
}

can I reliably destructure it:

local name, age = unpack(person)

My worry is that if the order of the fields in the table is changed, that destructuring assignment will no longer work as expected.


1 Answers

No, there's no way to associate table fields (which are just string keys in a hash table) with local variables of the same name. You just have to be explicit and think of variables and table keys as two separate things.

like image 124
luther Avatar answered Aug 28 '26 15:08

luther



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!