Axel Kittenberger
2014-06-04 14:48:06 UTC
Lua accepts since ever (as far I know) trailing commas in table definitions.
And this is good, since like most people experience sooner or later, when
doing multilined stuff taking care of the last element is kinda a hassle.
However, I never understood, why trailing commas are not allowed in
function calls.
Reasons are the same, sometimes a function call spawns several lines. This
happens for me especially with functions taking variable length arguments
like print() or write()
For example:
print(
'this is a string',
'another string',
'yet another string', -- note the comma here
)
I don't see any ambiguity added by this, or is there?
Patch is simple, maybe this one is a little naive, since explist() is
called on various places. But works as far I tested*
--- lua-5.2.3/src/lparser.c 2013-04-12 20:48:47.000000000 +0200
+++ lua-5.2.3-comma/src/lparser.c 2014-06-04 16:24:35.958824924 +0200
@@ -812,6 +812,7 @@
int n = 1; /* at least one expression */
expr(ls, v);
while (testnext(ls, ',')) {
+ if( ls->t.token == ')' ) break;
luaK_exp2nextreg(ls->fs, v);
expr(ls, v);
n++;
Kind regards, Axel
* test suite for available for 5.2.2 hangs my computer with extreme memory
usage at the same position "testing strings and string library" with or
without patch, dunno if my system is just too weak for it, I too impatient
or I'm doing something wrong.
And this is good, since like most people experience sooner or later, when
doing multilined stuff taking care of the last element is kinda a hassle.
However, I never understood, why trailing commas are not allowed in
function calls.
Reasons are the same, sometimes a function call spawns several lines. This
happens for me especially with functions taking variable length arguments
like print() or write()
For example:
print(
'this is a string',
'another string',
'yet another string', -- note the comma here
)
I don't see any ambiguity added by this, or is there?
Patch is simple, maybe this one is a little naive, since explist() is
called on various places. But works as far I tested*
--- lua-5.2.3/src/lparser.c 2013-04-12 20:48:47.000000000 +0200
+++ lua-5.2.3-comma/src/lparser.c 2014-06-04 16:24:35.958824924 +0200
@@ -812,6 +812,7 @@
int n = 1; /* at least one expression */
expr(ls, v);
while (testnext(ls, ',')) {
+ if( ls->t.token == ')' ) break;
luaK_exp2nextreg(ls->fs, v);
expr(ls, v);
n++;
Kind regards, Axel
* test suite for available for 5.2.2 hangs my computer with extreme memory
usage at the same position "testing strings and string library" with or
without patch, dunno if my system is just too weak for it, I too impatient
or I'm doing something wrong.