What is the best way to clone by value a class ins...
# multiplatform
c
What is the best way to clone by value a class instance on a multiplatform project (under the common code)? I’ve not found anything related to it, only the Cloneable on Java, but that doesn’t really help because I was trying to do it under the common part
g
You can use kotlinx.serialization
c
Thanks @gildor, is there any example on how can I accomplish this with serialization?
g
Serialize and then deserialize, it will be essentially copy. You can use simple map for this (something like this apporach - https://github.com/Kotlin/kotlinx.serialization/blob/master/examples/example-jvm/src/NamedMapDemo.kt)
But now I thinking, that you just can use Data classes. it will be just as straight forward solution
serialization will not work with some arbitrary class anyway
there is no way maigcally clone, as on JVM for example for native or js
so you need some interface for this
so data class or kotlin serialization provides it for you