Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

History of trailing comma in programming language grammars

Tags:

Many programming languages allow trailing commas in their grammar following the last item in a list. Supposedly this was done to simplify automatic code generation, which is understandable.

As an example, the following is a perfectly legal array initialization in Java (JLS 10.6 Array Initializers):

int[] a = { 1, 2, 3, };

I'm curious if anyone knows which language was first to allow trailing commas such as these. Apparently C had it as far back as 1985.

Also, if anybody knows other grammar "peculiarities" of modern programming languages, I'd be very interested in hearing about those also. I read that Perl and Python for example are even more liberal in allowing trailing commas in other parts of their grammar.

like image 523
polygenelubricants Avatar asked Feb 22 '10 15:02

polygenelubricants


People also ask

What is trailing commas?

A trailing comma, also known as a dangling or terminal comma, is a comma symbol that is typed after the last item of a list of elements. Since the introduction of the JavaScript language, trailing commas have been legal in array literals. Later, object literals joined arrays.

Why use trailing commas in JavaScript?

Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. If you want to add a new property, you can add a new line without modifying the previously last line if that line already uses a trailing comma.

Why are commas used in coding?

In the C and C++ programming languages, the comma operator (represented by the token , ) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there is a sequence point between these evaluations.


1 Answers

I'm not an expert on the commas, but I know that standard Pascal was very persnickity about semi-colons being statement separators, not terminators. That meant you had to be very very careful about where you put one if you didn't want to get yelled at by the compiler.

Later Pascal-esque languages (C, Modula-2, Ada, etc.) had their standards written to accept the odd extra semicolon without behaving like you'd just peed in the cake mix.

like image 64
T.E.D. Avatar answered Oct 01 '22 14:10

T.E.D.