Is it possible to something similar to be done in ...
# announcements
z
g
For Kotlin JS or for other targets too?
z
In Kotlin JVM actually
g
Depends on what is your JSON in JVM if instead of JSON you use Map<K, V> it’s easy, just sum 2 maps
z
What if I want to mutate just a specific part of the object but we have val on the underlying data class is this even possible.
Copy code
data class Family(val member: List<String>, val avgAge: Int, val origin: String)

// get from the database
val family =  Family(listOf("a", "b"), 10, "India")

val updatedFamily = family.apply { 
        origin = "UK"
}
c
family.copy(origin = "UK")
most object.assign usecases are very tied to how javascript objects work. so in kotlin most of the time you will use data classes and a copy constructor
or create your own dsl
v
Why would you create a copy constructor for a data class?
What is wrong with the copy function?
z
Thanks that makes sense, and I exactly wanted to this feature.
c
I’m talking about the autogenerated copy constructor
v
There is not auto-generated copy constructor
There is only an auto-generated copy function afaik
c
yeah its actually a copy method
👌 3
v
copy constructor:
MyDataClass(aDataClassInstance)
copy function:
aDataClassInstance.copy()
c
hmm yeah you are right. i thought its also called a copy constructor, but i was wrong
correct naming ftw!
v
Might seem like nit-picking, but I'm not a Kotlin expert and meant the question honest. 🙂
❤️ 2
z
Don't take things seriously have fun
c
no problem, it was useful to me because i know more than before now
👌 1
❤️ 1