Hacker News new | past | comments | ask | show | jobs | submit login

> (Note that the comma after the last value is accepted in Java and won’t cause an error.)

Why have I been 15 years programming in Java and I discovered this today?

On a more serious note, I prefer this kinds of posts to the traditional "Look at this shiny new thing" because without fundamental stuff the next big thing can't be built and normally this information makes you a slightly better programmer.




Likely because it's inconsistent with all other parts of the language where trailing commas are not allowed


Indeed. Whereas JavaScript and Python even allow a trailing comma in function parameters:

    function foo(x,) {}
    
    def foo(x,): pass
Before anyone questions why anyone would do this, it's for putting arguments (or array elements) on separate lines:

    function foo(
        a,
        b,
        c,
    ) {}


Although be careful with that in Python because there are other situations where (x) and (x,) are both legal but mean very different things.


In Python the comma acts as a tuple creation operator inside parentheses with no other context like function invocation.

Also, last I remember in JS a trailing comma meant the creation of an extra array element that’s null so I became conditioned to only use trailing commas for separators in languages where it’s consistent and specified very explicitly like in HCL


Tuple creation and destructuring does not require parentheses in Python.

     w   =  0,   # w == (0,)
    (x)  = (0,)  # x == (0,)
     y,  =  0,   # y == 0
    (z,) = (0,)  # z == 0
Though in Rust, parentheses are required for tuple creation and destructuring.


Trailing commas in JS don't do that, but having more commas than values does. (eg. [1,,].length === 2)


hey now, Enum allows trailing comma before semicolon, and ever since discovering that, I've been putting extra comma at the end of last Enum value and semicolon on new line.

Why? Because whenever there's a new value added, git diff shows single line change :)


Read a book on preparing for Java SE Certification, the exam is full of questions about things like this


I believe that comma thing was added recently.


Yup, as recent as 1996:

"A trailing comma may appear after the last expression in an array initializer and is ignored"

https://titanium.cs.berkeley.edu/doc/java-langspec-1.0/10.do...


The comma thing is inherited from C and has always been there. Newer bits of array-like syntax, like varargs and array literals in annotations, do not allow trailing commas.


I have this feeling the C comma operator was a accident from making function calls work in the beginning. After doing some hobby languages of my own I have come to realize many features are kinda automatic and accidental when writing languages, in a way other programming almost never is.


You may be thinking of javascript, which has added various kinds of trailing comma over the years, though arrays had them from the start. See https://stackoverflow.com/a/67793166


[flagged]


Did you ask yourself this question before you wrote this comment?


To answer your question: no, I didn't. It doesn't make sense to—I'm not here saying anything like, "I believe it's because X" without knowing whether X is true or not. If you think otherwise, explain how.

(I have now answered your question. If you respond, how about doing so in the form of an answer to mine—and not evading it with another question?)


I bothered to comment because I write Java for 15 years and I thought that my knowledge was relevant to this topic. I’m not going to check every my comment with reference documentation. In this particular case my memory failed me and I was quickly corrected and downvoted, so no harm was done, I guess, other than few people spending few minutes, for which I feel sorry, but not very much.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: