views:

309

answers:

6

Silly question, but why does the following line compile?

int[] i = new int[] {1,};

As you can see, I haven't entered in the second element and left a comma there. Still compiles even though you would expect it not to.

+7  A: 

It should compile by definition.

There is no second element. A trailing comma is valid syntax when defining a collection of items.

i is an array of int containing a single element, i[0] containing the value 1.

Mitch Wheat
That extra comma tells me another value is expected.
Sir Psycho
That may be your interpretation, but that's not how it is defined.
Mitch Wheat
That may be the case but it still doesn't make any sense regardless of the spec.
Sir Psycho
But it is still the answer. That snippet is legal because of the spec, and Jimmy gives the reasoning behind this decision.
Dykam
@Sir Psycho: Can't help there I'm afraid, that's how the designers of the language defined it.
Mitch Wheat
Go yell at everyone who wrote C, Java, and C# to change the spec.
Jon Limjap
I wonder how this answer to the question.
Luca
Thx for the 2 downvotes
Mitch Wheat
@Luca: the question was "why does this line compile?": Im pretty sure I explained that.
Mitch Wheat
@Sir Psycho: I can't help notice you have accepted the answer that quotes the spec you find so hard to swallow! lol
Mitch Wheat
Sir Psycho: When you write `int i; i++;` you don't say that the extra semicolon tells you that another statement is expected, do you? So why not allow the extra comma also?
Gabe
+5  A: 

its so you can do this and copy/paste lines around without worrying about deleting/adding the commas in the correct places.

int[] i = new[] { 
   someValue(),
   someOtherValue(),
   someOtherOtherValue(),

   // copy-pasted zomg! the commas don't hurt!
   someValue(),
   someOtherValue(),
   someOtherOtherValue(),

};
Jimmy
According to a similar post that <a href='http://stackoverflow.com/users/19750/jleedev'>jleedev</a> <a href='http://stackoverflow.com/questions/2311864/history-of-trailing-comma-in-programming-language-grammars'>pointed out</a>, its to make automatic code generation easier. Just caught me by supprise thats all :-)
Sir Psycho
+11  A: 

I suppose because the ECMA 334 standard say:

array-initializer:
    { variable-initializer-list(opt) }
    { variable-initializer-list , }
variable-initializer-list:
    variable-initializer
    variable-initializer-list , variable-initializer
variable-initializer:
    expression
    array-initializer

As you can see, the trailing comma is allowed:

{ variable-initializer-list , }
                            ↑

P.S. for a good answer (even if this fact was already pointed by many users). :)

Trailing comma could be used to ease the implementation of automatic code generators (generators can avoid to test for last element in initializer, since it should be written without the trailing comma) and conditional array initialization with preprocessor directives.

Luca
+8  A: 

This is syntax sugar. In particular, such record can be useful in code generation.

int[] i = new int[] {
    1,
    2,
    3,
};

Also, when you are writing like this, to add new line you need to add text only in single line.

Steck
Annoying that this is allowed in arrays, but not in Enums, where they would be much more useful.
Jon Limjap
it is allowed in enums (just tried in C# 3.0)
Mitch Wheat
IMO, this and Jimmy's answer should be edited together and then the result accepted! And I would add the observation that this is how `;` works between statements, for exactly the same reason. And that in Erlang, where the trailing symbol is different (a dot instead of a comma), it may appear comfortingly similar to English at first but users end up hating it for the same reasons given here.
Daniel Earwicker
It works in object initializers as well.
Frode N. Rosand
+6  A: 

Another benefit of allowing a trailing comma is in combination with preprocessor directives:

int[] i = new[] {
#if INCLUDE1
   1,
#endif

#if INCLUDE2
   2,
#endif

#if INCLUDE3
   3,
#endif
};

Without allowing a trailing comma, that would be much more difficult to write.

R Samuel Klatchko
+2  A: 

Same goes for enums:

enum Foo
{
  Bar,
  Baz,
};
leppie