Language feature request - trailing commas

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.

17 Likes

This was one of the features in our recent feature survey: Kotlin 1.1 Event Report | The Kotlin Blog

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.

4 Likes

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.

2 Likes

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.

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.

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.

+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.

1 Like

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.

1 Like

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?

4 Likes

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

1 Like

One of the most tempting cases of trailing commas is in listOf() and arrayOf() etc. If collection literals are implemented, I would say that there would be much less people concerned about this feature.

In addition, the poll actually forces people to choose two features (linebreak commas and trailing commas) together, which might not reflect opinions too well.

1 Like

@spungin I’m glad to read that, because I also wanted that :slight_smile:

I created a ticket for that, if you want to support it

1 Like