data class MyDataClass(
var a: String,
var b: Number? = null,
val c: Boolean,
private var d: Double
)
val myDataClass = MyDataClass(a = "hello", c = true, d = 1.20)
myDataClass.b = 1
myDataClass.a = "goodbye"
val (a,b,c) = myDataClass
println(listOf(a, b, c)) // [hello, null, true]
println(listOf(myDataClass.a, myDataClass.b, myDataClass.c)) // [goodbye, 1, true]
I would have expected destructuring my dataclass class instance to return [goodbye, 1, true]
as well but not only is it not returning updated values for it’s properties, it’s actually exhibiting two silent behaviors: it’s returning the passed in value for parameter a (“hello”), it’s returning the default value for parameter b (null). Basically two different behavior, and neither of these behaviors are documented on the Kotlin docs either under destructuring.
Most likely I’ve made a mistake, but if not, I would consider this to be non-intuitive behavior and likely to lead to bugs.
here’s my env: