https://kotlinlang.org logo
Title
d

daphillips

04/01/2020, 12:10 PM
Which is the better approach for
name
?
sealed class Things {
    abstract val name: String

    data class ThingA(val x: Int, val y: String) : Things() {
        override val name = "foo"
    }

    data class ThingB(z: Double) : Things() {
        override val name = "bar"
    }
}
or
sealed class Things(val name: String) {
    data class ThingA(val x: Int, val y: String) : Things("foo")

    data class ThingB(z: Double) : Things("bar")
}
2️⃣ 1
m

Mike

04/01/2020, 12:20 PM
I'd go with 2 for a number of reasons. The second tells me at a glance that all Things have a
name
, and it's also less code to read.
1
t

tddmonkey

04/01/2020, 12:59 PM
I prefer 2 also. We’ve done 1) and I’ve found it quite difficult to follow
d

daphillips

04/01/2020, 1:04 PM
Makes sense. Thanks for the input!