I have a Lua table and an index function like this:
curIndex = 0
function index()
curIndex = curIndex + 1
return curIndex
end
t = {
one = index(),
two = index(),
three = index(),
}
I’m aware that iterating over the table pairs may give me the keys “one”, “two”, “three” in any order. This created enough uncertainty despite experience and gut feeling to the contrary that I wanted to ask this question:
Is it guaranteed that the function index() is executed in the expected parsing sequence (one, two, three), so that I can rely on t.one having the index value 1, t.two == 2, t.three == 3 at all times?
Table constructor fields are evaluated in order. This guarantees that items added by index versus key are added in order. Your table constructor:
is equivalent to: