Hi all. I want to use my immutable kotlin data cla...
# getting-started
g
Hi all. I want to use my immutable kotlin data class in tests in my java code. In tests I initialize my data class with different values. In java I cannot use default parameters of the copy method. Also it seems to me too expensive to create multiple copies of the object while initializing it: each modification produces a new value. Ideally, I would like to use a builder to generate the object I need. Are there any tools to automatically create such a builder from an immutable data class or do I have to write it myself? edit: essentially to generate a "mutable" version of my data class.
e
yeah there's several, search for them
I made https://github.com/ephemient/builder-generator a long time ago, didn't maintain it
GitHub search finds https://github.com/beatgrid/kodeforge which looks more recent
g
thanks. In fact the builder itself is not needed. The only thing needed is to generate essentially a duplicate of the immutable data class but with all fields mutable.
then later I can use
apply
kotlin function to modify the object
Well, the builder is needed. But those methods to change individual fields not.
e
g
Something like:
Copy code
MyDataBuilder().apply{ /*.. modify builder properties here */ }.build()
thanks