Why is this forbidden?
# announcements
g
Why is this forbidden?
m
The compiler cannot create
copy()
or
component1()
here.
Just make it a regular
class
. No point in having a
data class
if there is no data 🤔
c
If you do actually want
DataClass
to be a data class, it might make more sense to have
MyClass
be an interface instead
g
@Marc Knaup It's just an example, I can have 15 other properties in data class, but it doesn't let me to add 1 more non
val
or
var
property
@Casey Brooks It can't be an interface, because I want to incapsulate field in it
m
Can’t you make the field
open
and use
override val
?
It’s not computed then though.
Otherwise you’ll have to drop the
data
and implement the needed functions yourself.
g
I can, but I want to incapsulate backing field in superclass
m
No way to do that then. The compiler won’t know what property of a supertype to use instead of the constructor property.
t
This is simply part of the language definition of data class. All data classes must only have property-based constructor parameters. As Mark mentioned - without this requirement the compiler wouldn’t be able to actually provide the guarantees that a data class implies (component decomposition, copy/equals/toString implementations, etc).
g
Just don't use this property to generate copy/equals etc
Like with properties that are located not in constructor
m
It cannot implement
copy
without knowing what value to pass for non-val constructor parameter
property
.
g
Yeah, I got it, thank you
j
data classes doesnt allow not use val/var I think