Repository navigation
feat: new page for data classes - #5885
Danil Pavlov (danil-pavlov) wants to merge 5 commits into
Conversation
| 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. |
There was a problem hiding this comment.
This feels really abstract to me. I would suggest to include references to things like "data-oriented programming".
| 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. |
There was a problem hiding this comment.
Thank you! added a reference to data-oriented programming
| * A parameter object that groups several related arguments. | ||
| * A result that consists of more than one value. |
There was a problem hiding this comment.
Those examples are also quite abstract.
| * 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. |
| 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. |
There was a problem hiding this comment.
| 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. |
| * 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). |
There was a problem hiding this comment.
Isn't it much simpler to just say it's final?
| | [`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. | |
There was a problem hiding this comment.
Note that with name-based destructuring this becomes way less important. It would be great if the documentation could hint this to users.
There was a problem hiding this comment.
Let's add "positional" here and a proper note to the destructuring section
| 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. |
There was a problem hiding this comment.
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.
| * 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()`. |
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
As mentioned above, please include a note to name-based destructuring becoming the default soon.
There was a problem hiding this comment.
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. | |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yeah, it's required by our guidelines, but I think the member description and the example right below cover the possible confusion
|
|
||
| #### Private properties | ||
|
|
||
| A property of the primary constructor can be `private`. The compiler still uses it in the generated functions: |
There was a problem hiding this comment.
| 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: |
There was a problem hiding this comment.
Maybe hint that IntelliJ can write this for you?
|
|
||
| ## Inheritance | ||
|
|
||
| A data class is always `final`, so **you can't inherit from a data class**: |
There was a problem hiding this comment.
| 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: |
| 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. |
There was a problem hiding this comment.
Those are all implications of being final, but the text makes it sound as they are somehow different.
No description provided.