wrkm.app
Search
Mobile Development

Value classes in Kotlin 2.5.0 and beyond

Kotlin 2.5.0 brings a long-awaited feature: full value classes! They are available in the Alpha stage, and in this blog post we’ll explain what full value classes are, how they compare to data classes, and how they connect to Project Valhalla on the JVM. Full value classes represent the next step towards ergonomic data-oriented programming […]

Editor
Dec 2, 2026 · 9 min read
Value classes in Kotlin 2.5.0 and beyond

Kotlin 2.5.0 brings a long-awaited feature: full value classes! They are available in the Alpha stage, and in this blog post we’ll explain what full value classes are, how they compare to data classes, and how they connect to Project Valhalla on the JVM.

Full value classes represent the next step towards ergonomic data-oriented programming and immutability in Kotlin. Nowadays, entire frameworks like Jetpack Compose are built on the notion of immutable data, and once mutability is gone, a big class of concurrency bugs disappears as well. At the same time, platforms like the JVM start bringing performance improvements for immutable data.

Full value classes are Kotlin’s answer to these trends. Our goal is to make the right thing also the most convenient thing: you write the code that is as concise as with data classes but get stronger safety guarantees and, potentially, better performance.

        Warning: Full value classes and related features are still in Alpha. That means they are not production-ready and breaking changes are expected. Our goal is to provide a solid ground for the community to experiment and provide feedback in realistic scenarios, before committing to the final design. Please try full value classes in your toy projects, especially if you’re using Valhalla alongside it, but maybe wait a while before deploying them.
    

TL;DR

Full value classes are immutable classes with an unrestricted amount of primary properties.

Full value classes are available on all platforms and JVM versions. Starting with JDK 28, Kotlin value classes can compile to JVM value classes, reaping all the performance benefits from Project Valhalla.

Kotlin 2.5.20 and later releases will bring improved diagnostics for identity, equality, and capture of value classes.

Value classes use name-based destructuring instead of position-based. Syntax for ergonomic updates (“a better copy”) is expected in Kotlin 2.6.0.

Inline value classes are becoming multiplatform in Kotlin 2.5.20, under a common inline modifier. Unless full value classes are enabled, every value class requires the inline modifier or the @JvmInline annotation, as was already the case for the JVM.

In this document we emphasize the distinction between full value classes and inline value classes, but in the future we shall refer to both as simply value classes.

Meet full value classes

From the syntax perspective, declaring a full value class looks no different than declaring a data class, except for the value modifier:

value class Point(val x: Double, val y: Double)

value class Rectangle(val from: Point, val to: Point) { init { require(from.x != to.x && from.y != to.y) { "This is a line!" } } }

On the conceptual level, the main difference compared to other kinds of classes is that an instance of a value class (full or inline) is completely determined by its primary properties and nothing else. This brings an important restriction: value classes can’t have properties with backing fields outside of the primary properties. You can still add properties, they just need to be computed. For example, we can provide a length property to Point using a getter:

value class Point(val x: Double, val y: Double) { val length: Double get() = Math.sqrt(xx, yy) }

We have also designed value classes to be as lean as possible. Compared to data classes, the compiler only generates code for equals(), hashCode(), and toString(), but not for copy() or componentN() functions.

No component and copy functions? No worries!

It’s true, value classes don’t have functions like copy() and componentN(). However, you can still use them just as ergonomically as data classes. We have been laying the groundwork for value classes over the previous Kotlin releases, and we’re still working on new solutions.

Destructuring of value classes uses name-based destructuring, which is stable in only-syntax mode from 2.5.0. Instead of using the position of primary properties, the compiler matches them by name to select the correct information. 

Since value classes are new to Kotlin, we enable name-based destructuring for them by default:

value class Rectangle(val from: Point, val to: Point) { val area: Double get() { // name-based destructuring val (x, y) = from

  // destructuring with renaming  
  (val otherX = x, val otherY = y) = to
  return Math.abs(x - otherX) * Math.abs(y - otherY)
}

}

The other main operation when working with immutable data is to create a copy of existing data with some properties modified. Copy properties, our solution for ergonomic immutable data updates, are in the final design stage. The goal is to make those operations look as natural as update operations in mutable data. We plan to ship the first Alpha version in Kotlin 2.6.0.

Given the benefits of value classes, we are working on simple migration strategies. Starting with Kotlin 2.5.20, library authors can mark an existing class with the @WillBecomeValue annotation to indicate the migration in the upcoming release. The compiler and IDE then warn you about the code that would break after migration.

What about value classes in the JVM (Valhalla)?

After more than a decade of work on Project Valhalla, the JVM ships value classes as a preview feature in JDK 28. We designed Kotlin value classes to benefit from any performance optimizations that this and the upcoming JDK releases bring.

However, full value classes are not limited to new JDKs. They are an integral part of the Kotlin language, and as such they’re supported on every platform where Kotlin runs, including older JDKs. That’s why there are two different compiler options related to full value classes:

-Xfull-value-classes enables the language feature.

-Xvalhalla-value-classes compiles Kotlin value classes into JVM value classes. JDK 28 or later is required for both compilation and execution.

Enabling full value classes in Gradle In order to enable these features in your project you need to add the compiler options to the compilerOptions{} block. This is how it looks in the case of Gradle:

kotlin { compilerOptions { freeCompilerArgs.add("-Xfull-value-classes") } }

For the Project Valhalla support, the recommended way is:

kotlin { jvmToolchain { languageVersion.set(JavaLanguageVersion.of(28)) } compilerOptions { freeCompilerArgs.add("-Xfull-value-classes", "-Xvalhalla-value-classes", "-Xjvm-preview") } } // to execute the 'main' function tasks.withType { jvmArgumentProviders.add(CommandLineArgumentProvider { listOf("--enable-preview") }) } // if you also have Java files in your project java { toolchain { languageVersion.set(JavaLanguageVersion.of(28))} } tasks.withType { options.compilerArgs.add("--enable-preview") }

What about other platforms?

Our first goal is to unlock the benefits of Valhalla, but value classes also provide optimization possibilities on other platforms. We’re investigating how to apply them in Native, JavaScript and WebAssembly. Android is a special case, since optimization of value classes depends on R8.

Value classes could also make Kotlin interop features more direct. That includes JavaScript, Swift Export, and C interop. These improvements are on our roadmap, but aren’t delivered in Alpha.

Are @JvmInline value classes deprecated?

No! In fact, we’re stabilizing them and aligning the behavior across different platforms. Starting with Kotlin 2.5.20, you can use the inline keyword instead of the old annotation: 

inline value class Age(val age: Int) { init { require(age >= 0) } }

For full context, you could already define value classes in Kotlin. However, among other restrictions, they can have only one single property and must be marked as @JvmInline on the JVM. Such value classes are inlined whenever possible: at runtime they’re represented by the type of their single property.

You might ask: “Why do you keep inlining when the runtime can optimize value classes anyway?” After an extensive investigation, it became clear that being inline is, in fact, an important characteristic. Serialization libraries, for example, treat inline classes differently. Interoperability with other platforms also needs to know exactly how instances are represented in memory.

As a result, if full value classes aren’t enabled, it means that a value class declared in the source code must be inline. To keep full forward compatibility, the compiler now requires either inline or @JvmInline on every platform, not only on the JVM platform.

Keep value semantics safe

As we already mentioned, we want to make data-oriented programming easy and intuitive for Kotlin developers. But sometimes appearances can deceive, so we’re investing in better analyses to catch surprising behavior in your code early.

Identity: Value classes don’t have an identity, they are fully described by data they wrap. This means that referential equality (===) and synchronization shouldn’t be used with them. In the JVM, the latter operation actually fails at runtime. The Kotlin compiler reports these mistakes before running your code.

Equality: Starting already with Kotlin 2.5.0, you can instruct the compiler to be stricter about equality operations (== and !=) by using the -Xstrict-equality-for-structural-classes. For example, comparing  a Point and a Rectangle produces a warning. This feature also covers data classes and enumerations, but value classes were our main motivation.

Lambda captures: Capturing value class variables is also tricky when different tasks access them concurrently. We’re working on a better analysis of capture by lambdas that covers all kinds of data, with value classes being a key use case.

Conclusion

Full value classes are a great leap forward in dealing with immutable and data-oriented programming in Kotlin. The Alpha release in Kotlin 2.5.0 lays the groundwork for a set of interrelated features, including better code generation in platforms that optimize value classes (such as the JVM from JDK 28 on), and improved diagnostics for value semantics.

The following timeline shows when each new feature mentioned in this blog post becomes available as Alpha. Further stabilization depends on community feedback.

Full value classes are a work in progress and we would like to hear from you. If you encounter any issues or want to share your feedback, let us know in YouTrack.

Kaynak: Alejandro Serrano Mena

#Kotlin
Share

From other categories