When a data class has a private property, destructing syntax do not work. For example, consider this data class
data class Product(
private val id: Int,
val name: String,
val description: String
)
Now if I try to use destructuring declaration, it throws compilation error.
val (_, name, description) = product // compilation error -> Cannot access 'fun component1(): Int': it is private
val (name, description) = product // compilation error -> Cannot access 'fun component1(): Int': it is private
It only works if I declare the private property at the end. But seems like a silly solution and not practical in all places.
data class Product(
val name: String,
val description: String,
private val id: Int
)
val (name, description) = product // works fine with private property at the end
Is destructuring even possible if the class has private property? If yes, then how? If not, why so?
When a data class has a private property, destructing syntax do not work. For example, consider this data class
data class Product(
private val id: Int,
val name: String,
val description: String
)
Now if I try to use destructuring declaration, it throws compilation error.
val (_, name, description) = product // compilation error -> Cannot access 'fun component1(): Int': it is private
val (name, description) = product // compilation error -> Cannot access 'fun component1(): Int': it is private
It only works if I declare the private property at the end. But seems like a silly solution and not practical in all places.
data class Product(
val name: String,
val description: String,
private val id: Int
)
val (name, description) = product // works fine with private property at the end
Is destructuring even possible if the class has private property? If yes, then how? If not, why so?
Share Improve this question edited Mar 28 at 7:36 devxgb asked Mar 27 at 19:42 devxgbdevxgb 2234 silver badges12 bronze badges2 Answers
Reset to default 2The reason why it doesn't work in general* is fairly self-explanatory from the way destructuring is explained in the docs: https://kotlinlang./docs/destructuring-declarations.html
It simply uses the generated componentN() methods on the class. Which have to follow the visibility modifier of the properties to not leak them. So in your case, you can only use destructuring inside the class itself, which I assume is the reason component methods are also generated for private members.
If you want to make it work you could either
Move private members to the end
Make it a regular class and write your own component1() and component2() implementations
* The case with the underscore is special though, and the doc linked above specifically says The componentN() operator functions are not called for the components that are skipped in this way.
Which would indicate that the private component1() method should not be called. So unless I am missing something that specific compiler error does sound like a bug.
One question is that all I do is create the destructuring function within the data class, in the case of classes when the values passed to them become variables you can access them from within, which allows me to access them with a function and how do I extract the function from the instance of the class to which the elements have already been passed, does it allow me to access the private element.