Sanniv Khare 7 min read

Kotlin 2.5 name-based destructuring: port positional code safely

Kotlin 2.5 name-based destructuring: port positional code safely

Upgrading to Kotlin 2.5 changes nothing about your existing val (a, b) = x lines: they stay positional. What 2.5 adds is a second syntax, (val a, val b) = x, that matches by property name, and a square-bracket form, val [a, b] = x, that is explicitly positional. The risk comes later, when the complete mode becomes the default and the old parentheses switch to name matching. Turn on -Xname-based-destructuring=name-mismatch now, fix every warning by switching to brackets or renaming, and the later switch becomes a no-op.

We checked this by compiling and running the same Kotlin/JVM file on Kotlin 2.4.21 and 2.5.0-RC (released 11 November 2026) in each mode, with Gradle 9.4.1 and JDK 17 on our M1 lab Mac. Kotlin 2.5.0 final is planned for December, according to the Kotlin releases page.

Why we did this: our Android code (the BMDB TV app, Peepal and Shopkeeper) destructures data classes, map entries and Compose state everywhere, and one of those patterns does not survive complete mode.

What 2.5.0-RC changes

The What’s new in Kotlin 2.5.0-RC page lists name-based destructuring in only-syntax mode as stable and the default. The destructuring docs describe the three values of the compiler option:

Mode What it does
only-syntax (default in 2.5) New syntax allowed, old val (a, b) keeps its positional meaning
name-mismatch Also warns where positional destructuring of a data class uses names that do not match the properties
complete Short val (a, b) matches by name; brackets stay positional

The same RC makes the unused return value checker stable and enabled by default, which shows up in the same build log.

The sample

One file, written the way our Android code usually is:

data class User(val username: String, val email: String)

// Same shape as Compose's MutableState: value plus a setter
class TextState(var value: String) {
    operator fun component1(): String = value
    operator fun component2(): (String) -> Unit = { value = it }
}

val (email, username) = user          // names in the "wrong" order
val (x, y) = Point(3, 4)
val (first, second) = "a" to 1
val (text, setText) = state           // componentN on a non-data class
for ((index, value) in list.withIndex()) { }
for ((k, v) in mapOf("k" to 1)) { }
names.sorted()                         // result dropped
"  padded ".trim()                     // result dropped

The first line is the trap. User declares username first, so positionally email receives the username.

Step 1: upgrade, and nothing moves

On 2.4.21 the file compiled with no warnings. On 2.5.0-RC with default settings it printed exactly the same output, including the swapped line (email=asha username=asha@example.com), plus two new warnings from the return value checker:

w: Main.kt:36:11 Unused return value of 'sorted'.
w: Main.kt:37:17 Unused return value of 'trim'.

So the upgrade itself is safe for destructuring. The two warnings are real bugs in the sample: sorted() returns a new list and the original stays unsorted. Setting -Xreturn-value-checker=full added nothing more for this file, because no result of our own functions was dropped.

Step 2: name-mismatch shows you the future

With -Pdestr=name-mismatch (our build passes it as -Xname-based-destructuring=name-mismatch), the compiler flagged three kinds of line:

Name-mismatch warnings from Kotlin 2.5.0-RC

Real output from our M1 lab Mac, shortened to fit.

  • The swapped data class: “Variable name ’email’ differs from accessed property name ‘username’… this code will change its meaning.”
  • The TextState destructuring: “This syntax will be used for name-based destructuring in a future release which will stop compiling or change its meaning for non-data class ‘TextState’.”
  • The map loop: “Variable name ‘k’ differs from accessed property name ‘key’.” Map entries are not data classes, but the compiler still checks them against key and value.

(first, second) on a Pair and (index, value) on withIndex() produced no warning, because the names already match the properties.

Step 3: what complete mode does to unported code

We then compiled the unported file with complete. It failed:

e: Main.kt:28:10 Unresolved reference 'text' on receiver of type 'TextState'.
e: Main.kt:28:16 Unresolved reference 'setText' on receiver of type 'TextState'.
e: Main.kt:33:11 Unresolved reference 'k' on receiver of type 'Map.Entry<String, Int>'.
e: Main.kt:33:14 Unresolved reference 'v' on receiver of type 'Map.Entry<String, Int>'.

Those are the easy ones, because the build stops. The data class line did not error. A smaller file with only the User and Pair lines compiled in both modes, and the output changed:

only-syntax:  email=asha username=asha@example.com
complete:     email=asha@example.com username=asha

Same source, same compiler, different values at runtime. In this sample the new result is the one the author meant, but in a real codebase you cannot assume that, which is why the warnings in step 2 are worth clearing before anyone flips the mode.

For Android code the TextState case matters most. val (text, setText) = remember { mutableStateOf("") } is a common Compose pattern, and MutableState is an interface with component1() and component2(), not a data class with properties of those names. Our stand-in class with the same shape stopped compiling in complete mode, so expect every such line in a Compose codebase to need the bracket form.

Step 4: port with brackets or explicit names

Positional destructuring before and after the port

The ported file, compiled on 2.5.0-RC.

The rule we used: if you meant “by position”, say so with brackets; if you meant “by name”, use the explicit form.

(val email, val username) = user            // by name
(val mail = email, val login = username) = user   // by name, renamed
val [x, y] = Point(3, 4)                     // by position
val [text, setText] = state                  // Compose-style state
for ([k, v] in mapOf("k" to 1)) { }
for ((val index, val value) in list.withIndex()) { }
listOf("a" to 1).forEach { [s, n] -> println("$s$n") }
val sorted = names.sorted()

This version compiled and ran with identical output in both only-syntax and complete on 2.5.0-RC, which is the point: once ported, the mode no longer matters. The bracket form also worked in for loops and lambda parameters.

On 2.4.21 the same file failed with “The feature ‘name based destructuring’ is only available since language version 2.5”, one error per new-syntax line. With -Xname-based-destructuring=only-syntax added, 2.4.21 compiled and ran it. That helps if one module has to stay on 2.4 for a while, but on 2.4 the feature is experimental, so we would not ship library code that depends on it.

Migration order we are following

  1. Move to Kotlin 2.5 (2.5.0 final when it lands in December). Fix the unused return value warnings; in our sample both were real bugs.
  2. Add the flag to every module and read the warnings:
kotlin {
    compilerOptions {
        freeCompilerArgs.add("-Xname-based-destructuring=name-mismatch")
    }
}
  1. For each warning, decide intent. Positional: switch to [a, b]. By name: rename the variables to the property names or use (val local = property). Do Compose mutableStateOf destructuring and Map.Entry loops first; they are the ones that stop compiling.
  2. Keep name-mismatch on in CI so new positional code with mismatched names gets flagged in review.
  3. When the toolchain moves to complete by default, the ported code needs no change.

What to check on your own app

  • Search for val ( and for (( in Compose code: every remember { mutableStateOf(...) } destructuring needs brackets.
  • Search for destructuring of your own data classes where the variable order differs from the constructor order. Those are the lines whose values will swap.
  • Generated code (Room, Moshi or KSP output) is compiled with the same flags; run a full build with name-mismatch, not only your own sources.

Timings are not the point of this post, but for reference: after the first download of the 2.5.0-RC compiler, each mode compiled and ran this single file in about a second on our lab Mac.

If you are on the Material3 side of an upgrade too, our post on porting TextField(value) to TextFieldState covers the other deprecation wave landing this month.

Made by the team behind this post

Apps from our studio

  • Goes with this post

    BMDB.fun

    Your Bollywood world at a glance, with links straight to the OTT app that streams each film.

    Android TV · Web · Mobile

    Get
  • Nix

    The Mac uninstaller that finds what the Trash leaves behind: caches, logs, derived data and saved states.

    macOS

    Get
  • Cardigami

    Pop-up greeting cards that rise out of the fold on iPhone Duo.

    iPhone · App Store

    Get
  • OneClick QuitAll

    Quit every running app in one click. See memory and CPU first.

    macOS · Mac App Store

    Get

Need this built? Let’s talk.

We build and run apps for Mac, TV, mobile and the web, and write about what we learn.