lua-users home
lua-l archive

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

On Sun, Mar 10, 2013 at 04:40:55PM -0600, Tim Mensch wrote:
> On 3/10/2013 4:39 AM, Ico wrote:
> >(As a side note: this last  comma is one of my annoyances with
> > json/javascript, I have numerous times made the same error when
> > trying to call some http API with JSON which I generated from Lua)
> Amen to that! The real question is why JSON/JavaScript DOESN'T allow a 
> trailing comma. It's rather dumb that it doesn't.

I think it's because various implementations handled them differently. Some
allowed, some disallowed. Some allowed but, e.g., added an additional
undefined value to an array in the presence of a trailing comma.

This is one of those things that should have been specified upfront (as it
is in Lua), but never was. When the behavior had to be standardized, the
only way to provide fully defined behavior which was backward compatible was
to disallow them entirely. I suppose they could have left it undefined, but
explicitly disallowing it put people on notice.

Another useful allowance to grant code generators are null statements. For
example, in C label constructs actually adorn statements, and can't be used

	for (;;)
	again: // invalid

But you can do something like

	for (;;) {
	again:; // valid


	for (;;) {
	again:(void)0; // valid void expression which documents intent

Note that the `for (;;)' is also using null statements. The void expression
pattern in the last example assures the reader that you didn't accidentally
omit a statement, which outside of for loop expressions is probably the
first conern that comes to mind when you spot a lone semicolon.

Lua has null statements, though not void expressions:

	while true do
	::again:: true --> invalid

	while true do
	::again:: --> valid

	function() true end --> invalid

	function() ; end --> valid

	function() end --> valid