Value Class in Kotlin
Kotlin's value
classes(formerly known as inline classes) are a powerful feature that can help you write more efficient and expressive code. In this blog post, we'll take a look at what value
classes are and how they differ from regular classes. We'll also explore some of the benefits and use cases for using value classes in your Kotlin projects.
What is a Value Class?
Sometimes it is necessary for business logic to create a wrapper around some type. However, it introduces runtime overhead due to additional heap allocations. Moreover, if the wrapped type is primitive, the performance hit is terrible, because primitive types are usually heavily optimized by the runtime, while their wrappers don’t get any special treatment.
To solve such issues, Kotlin introduces a special kind of class called an inline class. Inline classes are a subset of value-based classes. They don’t have an identity and can only hold values.
One of the key benefits of using value
class is that they can help you improve the performance of your code. Because value classes do not have any identity, the compiler can optimize them at runtime, and the JVM does not need to allocate memory or perform any additional operations for a value
class. This can lead to significant performance improvements, especially when working with large collections of data.
Another benefit of value classes is that they can be used to create type-safe, domain-specific types. For example, you might use a value class to create a type called PhoneNumber
that is guaranteed to be a valid phone number. This can help you catch errors early on in your development process and reduce the number of runtime errors in your application.
The value class in practice
To use a value
class in Kotlin, you need to define it using the value
keyword. For example, here's an example of a value class called FirstName
:
value class FirstName(val value: String)
If we are using Kotlin in the JVM, we have to use the @JvmInline
annotation before the class declaration
@JvmInline
value class FirstName(val value: String)
In this example, the FirstName
class is a value
class that wraps a string value. You can use the FirstName
class just like you would use a regular string, but it has a clear, meaningful name that makes your code more readable.
What can we use them for?
Since value
classes support some functionality from regular classes we can enforce them as value objects.
In the init
block we can add validation for the value so that the object is not created with invalid input. For example:
@JvmInline
value class FirstName(
val value: String,
) {
init {
require(value.isNotEmpty() && value.length > 2) { "Name should not be empty or less than 2 chars '$value'" }
}
}
In this case the FirstName
object will always represent a no empty String greater than 2 chars. trying to create an object with an empty name or less than two chars will through an IllegalArgumentException
.
Exception in thread "main" java.lang.IllegalArgumentException: Name should not be empty or less than 2 chars ''
Another important thing to keep in mind is that value classes can only have one property, which is the property that holds the underlying value.
Conclusion
In conclusion, value
classes are a powerful feature in Kotlin that can help you write more efficient and expressive code. They allow you to create type-safe, domain-specific types that can improve the performance of your code and make it easier to read and understand. If you're working on a Kotlin project, consider using value classes to improve the quality and maintainability of your code.