The Road to Name-Based Destructuring | The Kotlin Blog
Blog post from JetBrains
Kotlin is introducing a significant shift in its destructuring mechanism by moving from position-based to name-based destructuring, with the new syntax currently available in experimental form. This change aims to enhance the reliability and flexibility of destructuring, particularly when working with data classes, by allowing developers to extract properties by name rather than relying on their position. The new syntax enables the use of "val" inside parentheses for name-based destructuring and square brackets for positional destructuring, facilitating clearer and more maintainable code. The transition is gradual, with tools and compiler arguments provided to assist developers in migrating their code, and the feature is set to become stable by version 2.5.0, with a full transition expected by version 2.7.0. Despite the changes, Kotlin will continue to support position-based destructuring for certain use cases, such as collections and tuple-like structures, while also preparing for the introduction of multi-field value classes that will utilize name-based destructuring exclusively.