1. Overview

Since version 1.4, Kotlin has introduced a new feature: trailing comma support in enumerations.

In this quick tutorial, we’ll first understand what trailing comma support is and then discuss what benefit it can bring us.

2. What’s the Trailing Comma Support Feature?

When we write Kotlin code, we often need to write comma-separated enumerations, such as argument lists or destructuring declarations. Let’s see some examples:

val myList = listOf("one", "two", "three") 
data class Employee(val name:String, val age:Int, val department:String)
val (name, age) = Employee("Jerry", 42, "HR")

The code above shows three examples. In the first example, we’ve initialized a list. The parameter list for the listof() method is comma-separated. Similarly, in the second example, we’ve declared a data class called Employee, and the parameter list of its primary constructor is comma-separated. Finally, the last line is an example of destructuring declaration. (name, age) is comma-separated too.

If we look at the three comma-separated lists, we’ll see no trailing comma after the last element.

However, since version 1.4, Kotlin has supported trailing commas after the last elements. For example, we can write:

val myList = listOf("one", "two", "three",)

Well, that’s all about the trailing comma support feature. Next, let’s see what benefits this small change can bring us.

3. The Benefits of the Trailing Comma Support

In practice, when we write long parameter lists, usually we’ll use the multi-line syntax to make the code easier to read:

data class PersonWithoutTrailingComma(
    val name: String,
    val age: Int,
    val gender: String,
    val address: String,
    val nationality: String
)

We don’t have a trailing comma after the last constructor parameter nationality in the example above.

But, let’s say we need to add a new parameter, “dateOfBirth“. So, we need to first add a comma after the nationality argument and then append a new line with the new dateOfBirth argument:

data class PersonWithoutTrailingComma(
    ...
    val nationality: String,
    val dateOfBirth: LocalDate
)

Similarly, when we want to adjust the order of the argument list if the last argument is involved, we need to add or remove the trailing commas.

Now, if we write the argument list with the trailing comma support feature, we can just add, remove or swap arguments without taking care of the trailing commas.

For example, the PersonWithTrailingComma class looks like this:

data class PersonWithTrailingComma(
    val name: String,
    val age: Int,
    val gender: String,
    val address: String,
    val nationality: String,
)

Now, let’s swap the nationality and the address arguments and then add a new dateOfBirth argument at the end:

data class PersonWithTrailingComma(
    val name: String,
    val age: Int,
    val gender:String,
    val natinality: String,
    val address: String,
    val dateOfBirth: LocalDate,
)

We can see that we only focus on the arguments we need to add or change without thinking about which one is the last argument and which trailing commas we should add or remove.

4. Conclusion

In this short article, we’ve introduced Kotlin’s trailing comma support feature. It allows us to easily add or swap elements without adding or removing commas as we are coding. It’s particularly helpful when we use multi-line syntax for parameters or values.