3

Many languages have several rules like the following in their grammars, which forbid trailing commas:

call-expr: ident '(' expr-list? ')';
expr-list: expr | expr-list ',' expr;

However, the following formulation allows (but does not require) trailing commas, which has well-known advantages:

call-expr: ident '(' expr-list? expr? ')';
expr-list: expr ',' | expr-list expr ',';

Is there any downside to always using this, at least internally?

o11c
  • 13,564
  • 4
  • 46
  • 66
  • Perl allows trailing commas. – Laurel Apr 17 '16 at 20:28
  • Yes, I'm aware that many languages *do* allow it. – o11c Apr 17 '16 at 20:31
  • It's not something you'd likely know if you don't know that language. – Laurel Apr 17 '16 at 20:33
  • The downside is the possiblity that an amateur, entering a list of data, might leave a bit of data out without it being detected. I think optional trailing commas is an assumption the language users are expert, and your are trying to make their life easier. So whether it is a downside depends on who you think the typical user is. – Ira Baxter Apr 17 '16 at 21:11

2 Answers2

1

The downside is ambiguity where empty entries are permitted. Consider the following arrays in Javascript:

  • [1,,3]
  • [1,,3,]

Are they identical? Is their length identical? Should it be? (Answer: yes, but not in IE<=7)

Stefan Haustein
  • 17,024
  • 3
  • 30
  • 45
1

Languages which allow procedure parameters to be left off (I'm particularly thinking of the old HP systems language SPL; there may be others) would fail on this because the compiler would assume that there was a skipped parameter after the trailing comma which the procedure does not call for. I'll grant that this is a minor disadvantage - on the other hand, IMO the stated benefits are equally minor.