hello, i have a question related to pojos and how ...
# announcements
a
hello, i have a question related to pojos and how to create them in kotlin with data classes. we have a bean which gets successively filled with data in different methods. this bean has other beans nested. if i use val in data classes this seems to become an issue since i cannot reassign vals. on the other hand i want to avoid nullability. so i want to set a default value and later overwrite it. is there a pattern in kotlin for that?
u
snippet.kt
👍 1
val
is immutable. you need to use
var
c
Hmm. I’d probably be hesitant doing either of those recommendations. Not sure what your code is like but maybe the builder pattern could be applicable if you want to keep your model atomic? Contributing functions would contribute to a builder’s set function. The use site would receive the
builder.build()
a
i dont think this works, because it would have to be created at the same time, no?
i guess the var or lateinit approach are fine
u
If you are using a
data class
then
lateinit
won’t work for you. I also don’t think builder pattern solves your issue.
https://kotlinlang.org/docs/reference/data-classes.html In case you want to keep the objects immutable than you can use
copy
a
but copy seems bad; assume i want to change one val field in a nested bean...that would not look nice. i guess ur proposal for vars with default values makes most sense. i loose immutability but i still have non nullability