example
local a = {"Something",1,2,true}[2]
print(a)
-- expect result : 1
-- real result : error
Can anyone explain me about this?
example
local a = {"Something",1,2,true}[2]
print(a)
-- expect result : 1
-- real result : error
Can anyone explain me about this?
âLetâs see how many items I have bought! None?? I havenât been shopping yet!â
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?
local a = ({"Something",1,2,true})[2]
print(a)
Should do the job.
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.
So itâs like âHey, we need to do something inside the parentheses before we proceed the next thing.â, right?
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
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?
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.