https://kotlinlang.org logo
#announcements
Title
# announcements
t

thana

06/03/2019, 8:59 AM
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

gildor

06/03/2019, 9:03 AM
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

Pavlo Liapota

06/03/2019, 9:11 AM
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

thana

06/03/2019, 9:11 AM
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

gildor

06/03/2019, 9:14 AM
@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

Pavlo Liapota

06/03/2019, 9:19 AM
@gildor I agree
g

gildor

06/03/2019, 9:20 AM
they always have to redecalre those fields if they want to inherit
Isn’t it make sense? constructor argument is not always property
t

thana

06/03/2019, 9:22 AM
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

gildor

06/03/2019, 9:24 AM
how would it recognize it?
because of the same name?
t

thana

06/03/2019, 9:25 AM
yeah it might need a new modifier for that 😄
g

gildor

06/03/2019, 9:25 AM
or just override explicitly
using existing override modifier
If you have proposed syntax, you can discuss it in #language-proposals
t

thana

06/03/2019, 9:44 AM
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

gildor

06/03/2019, 9:45 AM
What is your use case for this?
t

thana

06/03/2019, 9:47 AM
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

gildor

06/03/2019, 9:48 AM
Maybe you just need some interface to avoid inheritance of abstract class?
or at least make
key
abstract
t

thana

06/03/2019, 9:50 AM
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

gildor

06/03/2019, 9:54 AM
Okay, let’s talk from another point than, why do you need data class for this?
t

thana

06/03/2019, 9:56 AM
it feels like events are data classes
they are immutable and should be comparable with each other based on their whole value
g

gildor

06/03/2019, 9:57 AM
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

thana

06/03/2019, 9:59 AM
maybe wee need a different shortcut to auto generate wquals/hashcode and tostring 🤔
like lombok does 🤦‍♂️
👍 1
2 Views