lua-users home
lua-l archive

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


Hi!

2014-05-14 10:46 GMT+02:00 Axel Kittenberger <axkibe@gmail.com>:
> local season = 's'
> ...
> local text =
>    ({
>        s = 'summer term',
>        w = 'winter term'
>    })[ season ]
>
> Why do I need brackets around the curly brackets?
>
> Why is
>
> local text =
>    {
>        s = 'summer term',
>        w = 'winter term'
>    }[ season ]
>
> not valid?

I think the syntax says so:

   var ::=  Name | prefixexp ‘[’ exp ‘]’ | prefixexp ‘.’ Name

and

    prefixexp ::= var | functioncall | ‘(’ exp ‘)’

Here, the expression in `exp` (being a table constructor) requires the
parentheses.

(Or: Because. I once noticed this too, but I was never inclined to ask why...)

Regards,
Matthias