`data classes` are not allowed to have non-propert...
# announcements
t
data classes
are not allowed to have non-properties in their primary constructors. otoh this means data classes cannot inherit from classes that require arguments in their own primary constructor. is there a way to overcome this issue? is this really intended? why?
g
data classes cannot inherit from classes that require arguments in their own primary constructorv
No, it’s of course possible, do you have some sample where it doesn’t work for you?
p
I guess he means:
Copy code
data class Bar(a: String) : Foo(a)
open class Foo(val a: String)
And as I know this is not allowed because all methods for a
data class
are generated based on properties in a constructor.
t
oh, yes, sorry, i want precise enough: the point is: they always have to redecalre those fields if they want to inherit
which OTOH causes problems e.g. with serialization AND might lead to boilerplate again
g
@Pavlo Liapota What is the point to do not expose a in data class? If you just want to override it, no problem with it, but it must be open:
Copy code
data class Bar(overrider val a: String) : Foo(a)
open class Foo(open val a: String)
p
@gildor I agree
g
they always have to redecalre those fields if they want to inherit
Isn’t it make sense? constructor argument is not always property
t
wouldn't it be cool if the copmpiler could just recognize that i do only override it and don't redeclare it? 🤔 Regarding to kotlinx.serialization it is a redeclaration as i have to makr the property in the base class @transient to avoid serialization errors
g
how would it recognize it?
because of the same name?
t
yeah it might need a new modifier for that 😄
g
or just override explicitly
using existing override modifier
If you have proposed syntax, you can discuss it in #language-proposals
t
thank you - chances are, we are just using data classes not the way, they are intended to be used 😄
oh an as long as serialization is the only problem it causes, there is no need to change the language, i think
g
What is your use case for this?
t
we have an abstract
DomainEvent
class which declares a
key
to identifiy the event. Then we have several concrete domain events which all inherit from
DomainEvent
g
Maybe you just need some interface to avoid inheritance of abstract class?
or at least make
key
abstract
t
yes we needed to make it abstract. but it kind of spoiles the interface of the inheriting classes. also the
DomainEvent
has other properties that are more or less of technical nature and nothing the implementations really should expose
g
Okay, let’s talk from another point than, why do you need data class for this?
t
it feels like events are data classes
they are immutable and should be comparable with each other based on their whole value
g
but standard class is also can be immutable
yeah, comparison make sense
serialization is problem of course, but not something unique for Kotlin or data classes
t
maybe wee need a different shortcut to auto generate wquals/hashcode and tostring 🤔
like lombok does 🤦‍♂️
👍 1