lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Sorry to revive an old thread. Here's an ambiguity you would introduce into the grammar if you implemented the rule
var ::=  Name | tableconstructor ‘[’ exp ‘]’ | prefixexp ‘[’ exp ‘]’ | prefixexp ‘.’ Name
Consider the following

function foo(_)
  return {"x", "y", "z"}
end
v = foo {"a", "b", "c"} [2]
print(type(v))

What is printed? If {"a", "b", "c"}[2] is evaluated first, then foo("b") is a table. If foo {"a", "b", "c"} is evaluated first, then {"x", "y", "z"}[2] is a string. The same ambiguous situation happens with the use of the dot operator and the colon operator.

> On 23.07.2017, at 8:07 AM, temp212@gorodok.net wrote:
> 
> Why a new table needs to be put in parentheses to index it?
> a =  {4, 3, 2, 1} [2]   --error
> a = ({4, 3, 2, 1})[2]   --ok
> at the same time length operator # works either way.
> 
>