I am looking at using sealed class to represent a finite set of possible values.
This is part of a codegeneration project that will write a very large number of such classes, which can each have a lot of cases. I am therefore concerned about app size. As it is very likely that several cases can have the same attributes, I am looking at using wrappers such as:
data class Foo(val title: String, ...lot of other attributes)
data class Bar(val id: Int, ...lot of other attributes)
sealed class ContentType {
class Case1(val value: Foo) : ContentType()
class Case2(val value: Bar) : ContentType()
// try to reduce app size by reusing the existing type,
// while preserving the semantic of a different case
class Case3(val value: Bar) : ContentType()
}
fun main() {
val content: ContentType = ContentType.Case1(Foo("hello"))
when(content) {
is ContentType.Case1 -> println(content.value.title)
is ContentType.Case2 -> println(content.value.id)
is ContentType.Case3 -> println(content.value.id)
}
}
Is this how I should approach this problem?
If so, how can I best make the properties of the associated value accessible from the sealed class? So that
is ContentType.Case2 -> println(content.value.id)
becomes
is ContentType.Case2 -> println(content.id)