Here is my code::
data class User(val id: Int, name: String)
fun getUsers(): List<User>
Now, I want to add an age
field to the User data class, but I'm concerned about two things:
- Breaking the code that already using
User
. - Loading memory with information that the user did not intend to request.
I would love to hear from the community about the best approach to add a field to a data class returned by a function in Kotlin without breaking existing code and without adding unnecessary memory overhead.
Thank you!
One approach I thought of is adding the age field with a default null, and then adding the information always or based on an optional argument in the getUsers
function
data class User(val id: Int, name: String, age: Int? = null)
fun getUsers(includeAge: Boolean = true): List<User>
However, I'm not sure if this approach solves the memory issue or not. Also, I'm considering the decorator approach, but I'm not sure if it's the best solution.