lua-users home
lua-l archive

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


> 1. Why does this not work?
>
> a = { 1, 2, 3, 4 } [1] --> Error: unexpected symbol near '['
>
> But this does:
>
> a = ({ 1, 2, 3, 4 }) [1]
>
> print (a)  --> 1

As you say in your subject, this is a syntactic issue. Because Lua does
not enforce the use of semicollons, it needs other means to recognize
statement boundaries. So, it is helpful to reduce the number of tokens
that can start a statement. For instance, if the first syntax were
allowed, people would expect the next one to be allowed too:

    {f, g, h}[1]("hi")    -- call 'f'

That would create ambiguities in the next fragment:

  a = i
  {f, g, h}[1]("hi")

It could mean any of these:

  a = i; {f, g, h}[1]("hi")
  a = i{f, g, h}[1]("hi")

Forcing the use of parentheses reduce the cases of ambiguity.

-- Roberto