suggestion: allow trailing commas to be optional (...
# language-proposals
x
suggestion: allow trailing commas to be optional (same way ; can be noise punctuation), the following code should compile, and behave the same way as if the last comma were not present
Copy code
val modules = listOf(
    "ph-db-migration",
    "ph-emr-entity",
    "ph-emr-event-type",
    "ph-events",
    "redox",
)
you probably wouldn’t write this like this, but it makes the last comma more obvious
Copy code
val modules = listOf( "ph-db-migration", "ph-emr-entity", "ph-emr-event-type", "ph-events", "redox",)
would compile to the same thing as
Copy code
val modules = listOf( "ph-db-migration", "ph-emr-entity", "ph-emr-event-type", "ph-events", "redox")
👍 7
2
💯 2
k
Yes!
o
How do you distinguish
a b c
between a list of 3 elements and a list of 1 element where
b
is infix function?
k
I think you misunderstood, this is specifically about making trailing commas optional instead of disallowed, not making all commas entirely optional like
;
.
👍 2
x
if you wanted the former, I might suggest perls’s “quote word” e.g.
qw( a ) b
, but yeah this is about trailing comma’s not lists of auto quoted words, symbols or other functions
updated the OP, to reflect that I’m talking about the last comma before the parens
r
@orangy I believe he only meant adding a comma after
"redox"
should be allowed, not making all commas optional.
👍 1
x
updated
o
Thanks, indeed I misunderstood. This is on the radar
x
@orangy so you’re saying this is already in the backlog to be added somewhere? maybe v1.4?
o
No promises, as usual. You can track it here https://youtrack.jetbrains.com/issue/KT-9476
👍 2
youtrack 1
p
I don't like that it's optional. It's just another thing to argue about when deciding on project conventions. I personally would not allow trailing commas in my projects and the compiler would no longer enforce it for me. Modern IDEs have no problem moving statements or parameters around and will handle the trailing commas automatically. This is a resounding 👎 from me.
x
@poohbar guess I need you to teach me how to use my IDE. I'm the opposite I would require a trailing comma on any vertically aligned "list", especially since when you changed the code you'd no longer get a 2 line difference in your diff, only the one you actually changed. To me the benefits would outweigh any concern over an extra character.
p
I guess I do. The action is called "Moved Element Right" (or Left) in IntelliJ and works flawlessly. Sure the diff will show the difference on 2 lines but that's more about the diff renderer. It can ignore that line if needed. Just like it can ignore whitespace etc. To me the benefits are minuscule and it just needlessly complicates the grammar.
k
It really annoys me that I have to deal with trailing
,
whenever I comment out a line in a multiline call, eg
Copy code
val x = listsOf(
    4654231,
    7898798,
    //1349846   -- oops, now there's a trailing comma!
)
I'd like the compiler to just carry on. The problem of conventions can then be solved like eg. spacing,
Ctrl+Alt+L
fixes it.
👍 1
p
Trailing commas are so ugly. 😕