Why can't we index a table that isn't yet define?

example

local a = {"Something",1,2,true}[2]
print(a)
-- expect result : 1
-- real result : error

Can anyone explain me about this?

2 Likes

“Let’s see how many items I have bought! None?? I haven’t been shopping yet!”

2 Likes

so the table acts like a list where it hasn’t been use yet, so if we store a list which means we have a list of item inside it and can be use later?

1 Like
local a = ({"Something",1,2,true})[2]
print(a)

Should do the job.

2 Likes

This is purely a limitation with Lua’s syntax. The only reason you can’t do it is that {…} is not a prefix expression, and indexing can only be done on a prefix expression. This is why putting in in parentheses causes it to work exactly how you’d expect. Any expression in parentheses is a prefix expression, but a table constructor is only an expression.

3 Likes

So it’s like “Hey, we need to do something inside the parentheses before we proceed the next thing.”, right?

1 Like

It does not work the same as ordering in math. Programming languages must use grammars that can be read unambiguously (having only one interpretation). There is no semantic reason why the code you originally posted can’t work, but adding that feature may have caused problems with telling it apart from another kind of statement. If you simply changed the language to allow any expression to be indexed, the following would be valid syntax, but nonsensical semantics:

false[1]
function myFunc()
end[2]
1 + 2[2]

The last example would additionally be ambiguous, you couldn’t tell whether the index is done to the expression 1+2 or just to 2

2 Likes

If I understand, {…} is a table which has ability to store item and check for value by index position. To get the value by index, we do “Table[num:array|any:dictionary]” which is the correct grammar. The reason why the table got an error because it’s not yet an expression. And that’s why we need to put parentheses to make it an expression (evaluated form of a table), am I correct?

And, if we have a variable that stores a table, the table would evaluates itself inside a variable and become an expression, so that we could work with it right?

1 Like

There can be some confusion between what the grammar allows and what the Lua machine can do. The language is turned into a sequence of operations, and those operations can create objects. The pieces of the language you write are not themselves the objects, but instructions to create them. A Table is an object that the machine can create whenever it wants, then assigning it to a new variable is a separate operation.
This statement creates a new table, then inserts four elements into it, then assigns ‘a’ to reference the new table. Notice that these things do not happen strictly left to right, even though the grammar makes it look that way.

local a = {"Something",1,2,true}

The brackets are a table constructor which tells the machine to create a table. This is an expression, so it’s allowed on the right side of an assignment. However, it’s not a prefix expression until you put the parentheses around it. The parentheses do not tell the machine to do anything, they only control how the statement is read, which determines what is and isn’t allowed to happen next. Indexing tries to operate on the previous prefix expression, and if there isn’t one, the language has nothing to do and must give up.
When you have a syntax error, the language does not know what operations to give to the machine, so it cannot even begin to run.

1 Like