Reasons to use JVM inline value classes

Mobile@Exxeta
3 min readJun 5, 2024

--

Photo by Sebastian Voortman from Pexels

Have you ever thought about a nice and fast way to describe simple data types? No? But there is one, and we will show you how an inline value class will be your next go-to option for this. Let’s dive into what this whole thing is about:

Inline value classes are a way to optimize performance and provide a way to represent small, immutable data. We will take a look at the advantages of such classes and compare them to other constructs in Kotlin, like data classes and type aliases.

Understanding JVM inline value classes

Using inline value classes is a way to represent simple and immutable data without the need for object allocation. By using the annotation @JvmInline, devs can tell the compiler to eliminate the wrapper class during the compilation time. With this, a more efficient bytecode and reduced runtime overhead are achieved.

An example could look like this:

@JvmInline 
value class UserId(val value: Long)

In this example, a Long is wrapped by the value class UserId. At runtime, instances of UserId are interpreted as if they were of type Long.

Here are the Reasons to use

  1. Performance Optimization
    With JVM inline value classes, there is no need to create additional objects. This leads to an improved performance, especially, when having a large amount of lightweight data. With this JVM inline value classes reduce memory consumption and improve the overall execution speed.
  2. Type Safety without overhead
    JVM inline value classes provide a type-safe way to represent simple data types without creating additional runtime objects.
  3. Interoperability with Java
    Kotlin’s inline value classes smoothly interoperate with Java code. This ensures that Kotlin devs can benefit from those classes when interacting with Java libraries and frameworks.

Let’s do a comparison with the alternatives

JVM inline value classes vs. Data classes

While both, JVM inline value classes and data classes, are designed for immutability, there are key differences between them. Data classes are more flexible and can be used for complex and nested objects involving more than one property. Inline value classes, in comparison, can only be used with a single property.

@JvmInline 
value class UserData(val value: Long)

data class UserData(val id: Long, val name: String)

JVM inline value classes vs. Type Aliases

Type aliases in Kotlin provide a simple way to assign different names to existing types; this leads to improved code readability. The downside is that they don’t eliminate the runtime overhead of creating wrapper objects. Another problem is that the alias and original type are assignment-compatible. This means that the compiler accepts the following code without an error:

typealias UserId = Long 
typealias OtherUserId = Long

fun setUserId(newId: UserId) { ... }

fun otherFunction() {
val otherId: OtherUserId = 10L
setUserId(otherId)
}

Inline value classes, on the other hand, provide a type-safe readable way to solve such cases and even address performance concerns by eliminating the need for runtime objects.

@JvmInline 
value class UserId(val value: Long)

typealias UserId = Long

Conclusion

JVM inline value classes in Kotlin are a powerful tool for optimizing the representation of lightweight, immutable data types. They strike a balance between performance and expressiveness, providing devs with an efficient way to model single-property abstractions without incurring the overhead of wrapper classes. When compared to data classes and type aliases, they stand out for their ability to deliver enhanced performance and type safety while maintaining an ergonomic syntax.

As Kotlin continues to evolve, the adoption of JVM inline value classes is likely to grow, further solidifying their place as a valuable language feature in modern software development.

So why don’t you try it out yourself and make your beautiful code even faster? (by Luca Hannemann)

--

--

Mobile@Exxeta

Passionate people @ Exxeta. Various topics around building great solutions for mobile devices. We enjoy: creating | sharing | exchanging. mobile@exxeta.com