David Lev
04/30/2023, 7:12 PMfun getUsers(): List<User>
data class User(val id: Int, name: String)
Now I want to add an age
field as well. But if I add I have 2 problems - I can break the code of users who use the User
construct, in addition - I load the memory (?) while information the user did not intend to request.
What I was thinking of doing is adding the 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)
But it doesn't solve the memory (or does it? How much does null takes?..)
Another option is the decorator method, but here only those who hear about the new class will benefit. The existing class will remain ageless.
In short - I would love to hear what options are available to me and what are the popular recommendations..
Thanks 🙂Francesc
04/30/2023, 8:14 PMdata class
you've backed yourself into a corner. If you intend this to be part of a public API, you should use regular classes and provide a factory method or builder pattern to instantiate the class. `data class`es come with copy, destructuring and other properties that make them binary incompatible when you make changes as described in your postAdam S
04/30/2023, 8:56 PMCLOVIS
05/01/2023, 5:34 PM