lua-users home
lua-l archive

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


I agree that forgetting commas in table-constructors this is a pretty
common annoyance (speaking from personal experience :P).

I think it's possible to make this smoother and more
beginner-friendly, without breaking *anything*:
Make commas optional for *consequent* <tkey> = <tvalue> 'assignments'
in table constructors.

Here is the relevant part of the Lua (5.1)-syntax:
tableconstructor ::= '{' [fieldlist] '}'
fieldlist ::= field {fieldsep field} [fieldsep]
field ::= '[' exp ']' '=' exp | Name '=' exp | exp

The only change would be:
field ::= tassign {tassign} | exp
tassign ::= '[' exp ']' '=' exp | Name '=' exp

I think this would allow to omit commas where they annoy most
("statement-like" table constructors), without introducing unnecessary
ambiguities.

Gregs initial example would only require 2 commas instead of 12 with
this change (AFAICT).
It would also be pretty simple to remember: Commas only needed when
constructing sequences (without specifying the keys explicitly).

Discuss! (I like this better and better the more I think about it; are
there any hidden flaws?)