Kotlin 2.5.0 Alpha Adds Full Value Classes: What Developers Should Know
Kotlin 2.5.0 ships a feature developers have waited a long time for: full value classes. It is still in Alpha. These are immutable classes with an unrestricted number of primary properties, and they are as easy to write as data classes.
JetBrains sees the industry moving toward immutable data. Frameworks like Jetpack Compose are built on that idea, and once mutability is gone, a whole class of concurrency bugs disappears with it. Meanwhile, platforms like the JVM are starting to offer performance improvements for immutable data. The goal is to make the right thing the most convenient thing: code as concise as a data class, with stronger safety guarantees and potentially better performance.
One important note: full value classes and related features are not production-ready, and breaking changes are still possible. JetBrains wants the community to try them in experimental projects, especially if you also use Valhalla, and send feedback before the final design is locked in.
What do full value classes look like?
Syntax-wise, the declaration looks like a data class, just prefixed with the value modifier. For example, value class Point(val x: Double, val y: Double). You can also add an init block for validation, such as a require check in a Rectangle class so its two points can't form a line. Conceptually, an instance is fully determined by its primary properties. That means a value class can't have properties with backing fields beyond the primary ones. Extra properties are fine as long as they are computed through a getter, like length on Point. The compiler only generates equals(), hashCode(), and toString(), with no copy() or componentN().
No copy or componentN, so what replaces them?
For destructuring, value classes use a name-based approach instead of a position-based one. The compiler matches primary properties by name. The only-syntax mode has been stable since 2.5.0 and is enabled by default for value classes. You can even rename variables while destructuring. For creating a copy of data with some properties changed, JetBrains is preparing copy properties. The design is in its final stage, and the first Alpha is planned for Kotlin 2.6.0.
For migration, starting with Kotlin 2.5.20 library authors can mark existing classes with the @WillBecomeValue annotation. The compiler and IDE then warn you about code that would break after migration. In the same release, inline value classes become multiplatform under a common inline modifier. Unless full value classes are enabled, every value class still requires the inline modifier or the @JvmInline annotation, as is already the case on the JVM.
How it connects to Valhalla on the JVM
After more than a decade of work on Project Valhalla, the JVM ships value classes as a preview feature in JDK 28. Kotlin value classes are designed to benefit from the performance optimizations in that JDK and later releases. Still, the feature isn't limited to new JDKs. It's part of the Kotlin language and is supported on every platform, including older JDKs. That's why there are two compiler options:
- -Xfull-value-classes enables the language feature.
- -Xvalhalla-value-classes compiles Kotlin value classes into JVM value classes. You need JDK 28 or later for both compilation and execution.
To enable them in Gradle, you add both options to your project's compiler configuration. Kotlin 2.5.20 and later releases will also bring improved diagnostics for identity, equality, and capture of value classes. For Kotlin developers, whether on Android or the backend, this is a good time to experiment in side projects. For apps already running in production, it's safer to wait until the feature leaves Alpha.
Source: blog.jetbrains.com