Language feature request - trailing commas


#1

Sometimes you have to create object with multiple parameters, for example:

data class Project(
    @JsonProperty(ID) val id: String?,
    @JsonProperty(TITLE) val title: String?,
    @JsonProperty(DESCRIPTION) val description: String?,
    @JsonProperty(STATUS) val status: Status?,

    @JsonProperty(LOGGING_STRATEGY) val loggingStrategy: LoggingStrategy?,
    @JsonProperty(CREATE_TASK_STRATEGY) val createTaskStrategy: CreateTaskStrategy?,
    @JsonProperty(VISIBILITY_STRATEGY) val visibilityStrategy: VisibilityStrategy?
) {}

Wouldn’t it be swell if we could append trailing comma to last parameter? (At least to constructors, but it would be nice for functions too)
This would help during adding new entries, reordering items and of course in version control.

It would be consistent with how we do enums now:

enum class Status {
    OPEN,
    CLOSED,
    PENDING,
}

It seems that it would not be a breaking change (to my untrained eye).
Is it relatively easy to do, or does it make grammar sigificantly more complex?

I sure would love me having this feature.


#2

This was one of the features in our recent feature survey: https://blog.jetbrains.com/kotlin/2017/04/kotlin-1-1-event-report/#more-4802

It doesn’t seem to be one of the most popular ones, so we’re not seeing this as a high priority issue, but it’s definitely something that we’re considering.


#3

Well, it’s not a great new shiny thing that attracts tons of attention. It’s just minor improvement, that makes coding flow considerably smoother. It’s good to know it is beeing considerered. I’ll be waiting!

EDIT:
Wow, you really have lots of amazing things to choose from.


#4

Scala introduced this feature recently in the version 2.12.2 (SIP-27 - Trailing Commas) and it breaks source compatibility with the version 2.12.1.


#5

Any new language features that we introduce into Kotlin are added in major releases (1.2, 1.3) and controlled by “language version” switches. The new syntax will be enabled only if you choose to switch your project to the new language version.


#6

This is nice discussion by the way, I will post it here just in case someone wants to check it out:

Yeah, in scala case it may cause lack of backward compability, but Kotlin is a bit different beast. In the end it all depends on implementation, there are many flavors of trailing commas.


#7

+1

I use this frequently when writing tests in Groovy to include/exclude test cases. It’s really nice to not have to always “clean up” that trailing comma.


#8

The survey results aren’t very good for this. See the blog post:

We shouldn’t have put “Optional commas” and “Optional trailing commas” under the same feature (my mistake, sorry), as it’s actually two features, so the results there are difficult to interpret.

I would have voted against optional commas, but the ability to put in trailing commas would be very nice indeed. Something I certainly miss from both C# and JS.


#9

On the related topic of complete comma removal…

I find myself often moving my class initializer parameters to and from class properties.

class Foo(
){
        // Properties
        var name:String = "Me"
        var age:Int = 15
        var happy:Boolean = true
}
class Foo(
        var name:String = "Me",
        var age:Int = 15,
        var happy:Boolean = true
){
        // Properties
}

Whenever I do this, I need to add or remove commas.

Obviously changing the order of items in the property list is also easier verses dealing with the parameters.

If Kotlin can eliminate semi-colons, why also rid the code of noisy commas in multiline lists?


#10

One of my favorite features from Python and Ruby. Really miss it here!