Kotlin Data Objects
Data objects just landed in with the Kotlin version 1.7.20 as an experimental feature.
You can try them out using Kotlin compiler with version 1.7.20 and -language-version 1.8 compiler argument.
Before checking out what a “data object” is and how it can be helpful, let’s remember what is an object declaration in Kotlin.
Object Declarations
An object declaration in Kotlin is a convenient way to create a well-defined singleton. They don’t have any constructors, can hold data, and can inherit from classes.
So when we only need to define a special instance of a class with some preset configuration, we inherit from that class and create an object instead of creating another subclass explicitly.
Also, creating another subclass means we may create multiple instances out of that subclass and that’s not always necessary.
Objects are great but they don’t pretty print their names.
Let’s see an example:
A common scenario for using objects is to create zero-property sealed class implementations.

The code above shows an AsyncResult sealed class and their implementations.
Objects are great here since using a subclass with zero constructors would be an overkill. However, when it comes to print them, they don’t look good.

To overcome this, one might override the toString() function and return a name but this also brings some boilerplate with so little value.

Data Objects
Kotlin intends to solve this with data objects.
Data objects have toString() functions implemented like data classes so that you don’t need to override toString() in objects anymore.

Overall, data objects are just objects with better toString() implementations which could be useful in logging or debugging.
Their equals() and hashCode() methods are not different than a normal object and also you can’t run copy() or componentN() functions on data objects like you can do on data classes.
You can read this chapter in Kotlin 1.7.20 changelogs or watch this video to learn more on the topic.
Please note that the data object feature is still experimental at the time this post is being written and may be subject to change.



