https://kotlinlang.org logo
#announcements
Title
# announcements
z

zain

12/16/2020, 10:32 AM
g

gildor

12/16/2020, 10:32 AM
For Kotlin JS or for other targets too?
z

zain

12/16/2020, 10:33 AM
In Kotlin JVM actually
g

gildor

12/16/2020, 10:33 AM
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

zain

12/16/2020, 10:40 AM
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

christophsturm

12/16/2020, 10:41 AM
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

Vampire

12/16/2020, 10:44 AM
Why would you create a copy constructor for a data class?
What is wrong with the copy function?
z

zain

12/16/2020, 10:44 AM
Thanks that makes sense, and I exactly wanted to this feature.
c

christophsturm

12/16/2020, 10:44 AM
I’m talking about the autogenerated copy constructor
v

Vampire

12/16/2020, 10:44 AM
There is not auto-generated copy constructor
There is only an auto-generated copy function afaik
c

christophsturm

12/16/2020, 10:45 AM
yeah its actually a copy method
👌 3
v

Vampire

12/16/2020, 10:45 AM
copy constructor:
MyDataClass(aDataClassInstance)
copy function:
aDataClassInstance.copy()
c

christophsturm

12/16/2020, 10:47 AM
hmm yeah you are right. i thought its also called a copy constructor, but i was wrong
correct naming ftw!
v

Vampire

12/16/2020, 10:48 AM
Might seem like nit-picking, but I'm not a Kotlin expert and meant the question honest. 🙂
❤️ 2
z

zain

12/16/2020, 10:49 AM
Don't take things seriously have fun
c

christophsturm

12/16/2020, 10:49 AM
no problem, it was useful to me because i know more than before now
👌 1
❤️ 1