Hello, I'm getting really confused about a type mismatch error:
Copy code
abstract class X
data class Y() : X(), Serializable
data class Z() : X(), Serializable
var firstValue: X = Y() -> Type mismatch
var secondValue: X = Z() -> Type mismatch
Is there something I am missing?
d
diesieben07
08/11/2020, 1:54 PM
Your code does not compile, because data classes must have at least one primary constructor parameter. I don't get the "type mismatch" error.
➕ 2
m
Miguel
08/11/2020, 1:55 PM
I do have constructor parameters in the data classes, I just removed them for the sake of question.
Miguel
08/11/2020, 1:56 PM
I'm getting the type mismatch errors when I try to instantiate the variables and I don't get it why.
d
deactivateduser
08/11/2020, 1:59 PM
usually type mismatch comes with more detailed error message... you have any?
s
StavFX
08/11/2020, 8:44 PM
This code compiles and runs
Copy code
abstract class X
data class Y(val y: Int) : X(), Serializable
data class Z(val z: String) : X(), Serializable
var firstValue: X = Y(0)
var secondValue: X = Z("")