Avatar ·

How to override a getter for a field in a Kotlin data class?

Hello everyone! I'm learning Kotlin and encountered a problem while working with data classes. \n\nI have a simple data class for storing user information:\n\n```kotlin\ndata class User(\n val firstName: String,\n val lastName: String,\n val age: Int\n)\n```\n\nI want to add a computed field `fullName` that returns `"$firstName $lastName"`. I thought I could simply override the getter for the field, but the compiler complains when I try something like:\n\n```kotlin\ndata class User(\n val firstName: String,\n val lastName: String,\n val age: Int\n) {\n val fullName: String\n get() = "$firstName $lastName"\n}\n```\n\nThis works, but I want `fullName` to be part of the data class constructor and still have a custom getter. For example, I want