Is it possible to nest dataclass in kotlin?

Viewed 8826
1 Answers

No, Kotlin does not support anonymous structures like that.

You can both literally nest the classes:

data class A(
    val b: Int,
    val c: C
) {
    data class C(
        val d: Int
    )
}

Or use a more common syntax:

data class C(
    val d: Int
)

data class A(
    val b: Int,
    val c: C
)

Actually, there is no need in "nesting" here. The difference will be mostly in the way you access the C class: A.C or just C.

Related