skip to content

What are trailing commas in Kotlin, where are they allowed, and why are they useful?

level: middleimportance: should knowfreq 35%

answer

  1. Comma after the last element
  2. Allowed since Kotlin 1.4
  3. Params, args, when, enums, destructuring, lambdas
  4. Benefit: clean diffs + easy reordering
  5. Zero runtime effect; ktlint/detekt enforce

basics

~20 s

A trailing comma is a comma after the last item in a list, like f(a, b,). Kotlin allows it in many comma-separated lists. It makes adding/removing/reordering lines easier and produces cleaner diffs in version control.

solid answer

~40 s

Kotlin (since 1.4) permits an optional **trailing comma** after the final element of most comma-separated lists: function/constructor **parameters** and **call arguments**, **value/type parameter** lists, `when` entry conditions, collection/array literals in annotations, destructuring declarations, lambda parameters, and `enum` constant lists. It is *not* allowed everywhere — e.g. not after a vararg spread in some positions or where a single element is required. The benefit is mechanical: each element sits on its own line ending in a comma, so adding, removing, or reordering lines touches only those lines, yielding minimal, clean VCS diffs and easier code generation. `ktlint`/`detekt` can enforce a consistent trailing-comma policy. It changes nothing at runtime; it's purely a source-formatting convenience.

code

kotlin · 10 lines
kotlin
fun build(
    host: String,
    port: Int,
    secure: Boolean,   // trailing comma keeps future diffs to one line
) = Unit

val color = when (code) {
    1, 2, -> "low"     // trailing comma in when conditions
    else -> "high"
}

go deeper

for a junior

Recognizes the syntax and that Kotlin permits it where Java often doesn't.

for a middle

Lists several allowed positions and explains the clean-diff/reordering benefit.

for a senior

Knows it's a 1.4 feature, that it has zero runtime impact, and how ktlint/detekt enforce a consistent policy.

for a principal

Frames it as a tooling/codegen and reviewability decision, standardizing it across a team's style config to reduce merge friction.

## What it is A **trailing comma** is a comma placed after the *last* element of a comma-separated list: ```kotlin fun create( name: String, age: Int, // <- trailing comma, allowed ) { } ``` ## Where it's allowed (Kotlin 1.4+) Common positions include: - Function and constructor **parameter** lists - Function/constructor **call argument** lists - **Type parameter** lists `<A, B,>` - **`when`** entry conditions: `1, 2, -> ...` - **Collection/array** literals inside annotations - **Lambda parameter** lists: `{ a, b, -> ... }` - **Destructuring** declarations: `val (x, y,) = pair` - **`enum`** constant lists - **Indexing** with multiple indices `arr[i, j,]` ## Where it is NOT allowed It can't appear where a trailing comma would be ambiguous or where only a single element is permitted — e.g. after `vararg` in certain spread positions, or in places expecting exactly one value. When in doubt the compiler will tell you. ## Why it's useful 1. **Clean diffs** — adding/removing the last item changes one line, not two (the previous line no longer needs a comma added/removed): ```kotlin // add 'BLUE,' = one-line diff because GREEN already ends with a comma enum class C { RED, GREEN, BLUE, } ``` 2. **Easy reordering** — every line is structurally identical (item + comma), so you can sort or move lines freely. 3. **Code generation** — generators don't need special-casing for the last element. ## Tooling `ktlint` and `detekt` support a **trailing-comma** rule and can auto-insert/remove them to keep a codebase consistent; IntelliJ also formats them. The convention is becoming a default in many style guides for multi-line lists. ## Runtime impact **None.** Trailing commas are purely lexical/formatting sugar — they don't appear in bytecode or change semantics.

  • Does a trailing comma affect runtime behavior or bytecode?
    No — it's purely source formatting. The compiled output is identical with or without it.
  • Name two non-obvious places trailing commas are allowed.
    `when` entry conditions (`1, 2, ->`) and destructuring declarations (`val (a, b,) = ...`); also lambda parameter lists and type-parameter lists.

Like leaving a blank line after every paragraph so you can paste a new one in without retouching the one above.

saying these in an interview costs you the question

  • Claiming trailing commas change runtime behavior
  • Saying they were always supported (they arrived in 1.4)
  • Thinking they're allowed literally everywhere with no exceptions
  • Calling them a compile error in parameter lists
  • Confusing them with Java, which disallows them in arg lists

context