How would you copy the whole content of class A to...
# announcements
r
How would you copy the whole content of class A to class B data class A(val name:String, val list:List<A>) to data class B(val name:String, val list:List<B>)
l
make both inherit from a common interface ?
r
why do i need a interface there?
can i not do deep copy here
l
...sry i didnt think this through. not sure if an interface actually helps with this
a
Not sure of kotlin, but in scala their is one library which help to achieve this may be something similar exists for kotlin as well. https://scalalandio.github.io/chimney/
h
Given classes
A
and
B
are implementing the same interface and you define the data class as
data class Example(val name: String, val list:List<InterfaceName>)
you get what you need and only need one data class 😉
Which seems to be the best thing anyways
Otherwise you should have a transformation method to get from
A
to
B
but I don’t see why you would do that 😉
l
why do you have 2 different data classes with the same structure anyway?
👍 1
r
I have a common kotlin module, which has a data class, but it needs to be parcelable. For that this common module module needs to be turn into android module which i dont want. So i was thinking of copying this to other data class in android module through mapper and make the other class Parcelable.
I found this, going to try it out, then maybe i dont have to do this
Don't thik this is going to android, I need a mapper in between one layer to other layer. Any easy way to acheive the deep copy here.
h
Well, manual mapping might be the only possible way then. Unless you want to get out the big guns like mapstruct.
You could just have a shared data structure for both sides
see it as DTO