Skip to content

feat: new page for data classes - #5885

Open
Danil Pavlov (danil-pavlov) wants to merge 5 commits into
masterfrom
new-data-classes
Open

Danil Pavlov (danil-pavlov) wants to merge 5 commits into
masterfrom
new-data-classes

Conversation

@danil-pavlov

Copy link
Copy Markdown
Contributor

No description provided.

@danil-pavlov
Danil Pavlov (danil-pavlov) requested a review from a team as a code owner October 23, 2026 18:02
Comment thread docs/topics/data-classes.md Outdated
Data classes in Kotlin are primarily used to hold data. For each data class, the compiler automatically generates
additional member functions that allow you to print an instance to readable output, compare instances, copy instances, and more.
Data classes are marked with `data`:
A data class is a class whose main job is to hold data.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels really abstract to me. I would suggest to include references to things like "data-oriented programming".

Suggested change
A data class is a class whose main job is to hold data.
A data class is a kind of class optimized for holding data, as opposed to packing related behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! added a reference to data-oriented programming

Comment thread docs/topics/data-classes.md Outdated
Comment on lines +9 to +10
* A parameter object that groups several related arguments.
* A result that consists of more than one value.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those examples are also quite abstract.

Suggested change
* A parameter object that groups several related arguments.
* A result that consists of more than one value.
* The description of a command or event in your system.
* A result with more than one value, like the quotient and remainder of a division.

Comment thread docs/topics/data-classes.md Outdated
Comment on lines +12 to +15
Compared to regular classes, where printing an instance shows just the class name and an identifier, and two instances
are equal only if they are the same object, data classes benefit from [pre-generated functions](#generated-functions).
They make it convenient to print an instance as readable text, compare two instances by their content, and copy an instance
with some values changed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Compared to regular classes, where printing an instance shows just the class name and an identifier, and two instances
are equal only if they are the same object, data classes benefit from [pre-generated functions](#generated-functions).
They make it convenient to print an instance as readable text, compare two instances by their content, and copy an instance
with some values changed.
The crucial different with regular classes is that an instance of a data class is in principle determined by the _data_ its holds, as opposed to the identity of the instance. This shows in the [pre-generated functions](#generated-functions): printing an instance of a data class shows all the primary properties, and equality ensures that two instances holding the same data are considered equal for all purposes.

Comment thread docs/topics/data-classes.md Outdated
* The primary constructor must have at least one parameter.
* All primary constructor parameters must be marked as `val` or `var`.
* Data classes can't be abstract, open, sealed, or inner.
* A data class can't be abstract, open, sealed, or inner, and it [can't be inherited from](#inheritance).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it much simpler to just say it's final?

Comment thread docs/topics/data-classes.md Outdated
| [`hashCode()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/hash-code.html) | Returns a hash code computed from the properties of the primary constructor, so that instances work as elements of a set and as keys in a map. |
| [`toString()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/to-string.html) | Returns a string with the class name and the properties of the primary constructor, such as `User(name=John, age=42)`. |
| [`copy()`](#copying) | Returns a new instance with the same properties, except for the ones that you pass as arguments. |
| [`componentN()`](destructuring-declarations.md) | Returns the property at the given position, which makes [destructuring declarations](#destructuring) possible. |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that with name-based destructuring this becomes way less important. It would be great if the documentation could hint this to users.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add "positional" here and a proper note to the destructuring section

Comment thread docs/topics/data-classes.md Outdated
Comment on lines +102 to +103
Data classes have no other generated functions, and they inherit the rest of their members from
[`Any`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/), the superclass of every Kotlin class.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that data classes may inherit from other interfaces or classes. This paragraph makes it sound as if they could not, and only get Any as a superclass. Personally, I would completely drop this paragraph.

Comment thread docs/topics/data-classes.md Outdated
* All primary constructor parameters must be marked as `val` or `var`.
* Data classes can't be abstract, open, sealed, or inner.
* A data class can't be abstract, open, sealed, or inner, and it [can't be inherited from](#inheritance).
* You can't provide explicit implementations of the [generated functions](#generated-functions) like `componentN()` and `copy()`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not true, you can still provide custom equals, hashCode and toString


### Destructuring

The `componentN()` functions generated for data classes make it possible to use them in [destructuring declarations](destructuring-declarations.md).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above, please include a note to name-based destructuring becoming the default soon.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, when name-based destructuring becomes default, we can rewrite this section and change the code sample


| Member | Description |
|------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| [`equals()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/equals.html) | Returns `true` if the other instance is of the same type and all the properties of the primary constructor are equal. Used by the `==` operator. |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: are () really required after the member name? I am a bit concerned that copy() is not really a no-argument function, so people may get confused.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's required by our guidelines, but I think the member description and the example right below cover the possible confusion

Comment thread docs/topics/data-classes.md Outdated

#### Private properties

A property of the primary constructor can be `private`. The compiler still uses it in the generated functions:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A property of the primary constructor can be `private`. The compiler still uses it in the generated functions:
A primary constructor property can be `private`. The compiler still uses it in the generated functions:

We tend to use that wording more often.

{kotlin-runnable="true"}

For this reason, prefer `List` over `Array` in data classes. If you have to store an array, for example, because an
external API returns one, write `equals()`, `hashCode()`, and `toString()` yourself and compare the content explicitly:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe hint that IntelliJ can write this for you?

Comment thread docs/topics/data-classes.md Outdated

## Inheritance

A data class is always `final`, so **you can't inherit from a data class**:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A data class is always `final`, so **you can't inherit from a data class**:
A data class is always `final`, so you can't inherit **from** a data class:

Comment thread docs/topics/data-classes.md Outdated
Comment on lines +574 to +577
The limitations are the following:

* A data class can't be marked as `open`, `abstract`, `sealed`, or `inner`, so no other class can extend it.
* A data class can't be the supertype of another class, not even of another data class.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are all implications of being final, but the text makes it sound as they are somehow different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants