KyooSik Lee
10/23/2019, 4:55 AMkotlin
data class Foo(
val a: Int,
val b: String,
val c: Int
val d: Int
) {}
And I have another data class partialFoo, where I have partial fields of foo
kotlin
data class PartialFoo(
val a: Int,
val b: String
) {}
Is there any way that I can copy the fields of class instance of Foo
to class instance of PartialFoo
?Kirill Zhukov
10/23/2019, 4:59 AMKirill Zhukov
10/23/2019, 4:59 AMFoo.toPartialFoo()
might the most idiomaticKirill Zhukov
10/23/2019, 5:00 AMKirill Zhukov
10/23/2019, 5:00 AMKyooSik Lee
10/23/2019, 5:01 AMKirill Zhukov
10/23/2019, 5:01 AMKirill Zhukov
10/23/2019, 5:02 AMjackson
Kirill Zhukov
10/23/2019, 5:02 AMFoo
object to json and then deserialize it to PartialFoo
Kirill Zhukov
10/23/2019, 5:02 AMKirill Zhukov
10/23/2019, 5:03 AMKyooSik Lee
10/23/2019, 5:03 AMKyooSik Lee
10/23/2019, 5:03 AMKyooSik Lee
10/23/2019, 5:04 AMKirill Zhukov
10/23/2019, 5:04 AMKirill Zhukov
10/23/2019, 5:06 AMFoo
or PartialFoo
changes you won’t catch until in runtime or in unit tests if you have them. You could remedy that by using hardcoded serialized names but that’s just too much trouble and still doesn’t prevent someone from deleting the field or changing the type.Kirill Zhukov
10/23/2019, 5:06 AMKirill Zhukov
10/23/2019, 5:06 AMbodiam
10/23/2019, 6:46 AMStephan Schroeder
10/23/2019, 12:56 PM