How would you have a data class inheriting a seale...
# announcements
j
How would you have a data class inheriting a sealed class and its variables without having to pass them, something like
Copy code
sealed class Source(
    @Json(name = "value")
    val value: String,
    @Json(name = "value2")
    val value2: Boolean
) {
    object Source1: Source
    object Source2: Source
}
m
Unfortunately you can't do it cleanly.
j
Which other way would you do it
s
if you don’t want to pass them through the constructor, you can make them abstract vals in the parent class and implement them in the objects
j
I want to avoid declaring them in all the implementations
In my case I have 10 implementations (Sources)
s
the constructor approach is probably the cleanest imo, and you’re going to have to set those values anyhow right?
Copy code
object Source1 : Source(
        value = "value",
        value2 = true
    )

    object Source2 : Source(
        value = "other_value",
        value2 = false
    )
j
No, they will be set by retrofit and the moshi adapter
Copy code
@JsonClass(generateAdapter = true)
data class Response(
    @Json(name = "source1")
    val source1: Source?,
    @Json(name = "source2")
    val source2: Source?,
    @Json(name = "source3")
    val source3: Source,
    ...
)
Found a solution 👍
setting default values on the base
Source
’s variables