is there a feature in between it being a 'data' cl...
# getting-started
s
is there a feature in between it being a 'data' class and a normal class? how do i get kotlin to generate componentN's if it is not a data class?
e
it won't generate them, but you can write them by hand
Copy code
operator fun component1()
s
yeah but you'd have to make a definition for it, yeah? that's a shame. seems like they should add that. not everything can be a data class
though i do see that there are some assumptions they can make if it is a data class (e.g. it cannot be open if it's data), so maybe that helps something language or jvm wise...
a
@sreich how do you decide whether to make
component1()
or not ? Do you suggest to add it to every class ?
a
I have a similar wish but for the copy method. Sometimes, I just need a copy method and have to change my whole regular class into a data class just to avoid writing copy by hand
a
Well, "just need a copy method" is a big deal.
2
j
Honestly, destructuring declarations are dangerous because they are position-based. I would only use
componentN
functions on very simple classes for which the properties' positions are obvious. For instance, a key-value
Pair
, a triple, a 2D point, etc. In those cases, very often
data class
makes sense.
i do see that there are some assumptions they can make if it is a data class (e.g. it cannot be open if it's data)
Exactly. Extending a class with new properties would make the order in the destructuring declarations even less obvious to the user
yeah but you'd have to make a definition for it, yeah? that's a shame. seems like they should add that. not everything can be a data class
Do you think
operator fun component1() = myProp1
is really too long? I believe it's not worth complicating the language for this, especially when we know positional destructuring has pitfalls already and should be used with care
s
hmmm I see the validity in your points
j
not everything can be a data class
By the way, if what blocks you here is some limitation on
data class
like inheritance, you might also consider extracting the set of properties that you want to destructure as a separate
data class
and use that in your original class instead of the set of properties. Not sure if that makes sense in your specific case, though