Just wondering, if there's sealed interfaces, what...
# announcements
d
Just wondering, if there's sealed interfaces, what are the use cases for sealed classes now?
h
I would say the same use case as with non sealed things: whenever you need a class and no interface. For example for state, as interfaces can only have abstract state. But yea, i somehow agree, most of at least my use cases seem to be covered by interfaces now.
d
I never really understood the need for (nor even how to use...) the parent instance in sealed classes... my use cases never needed them. But maybe someone did find something useful I'm missing out?
j
you can share a state in all subclasses, because it is shared, you havent to check the subtype to know it has the state
For example, if all branches has a name variable and you are not using it in the parent, you can't access to it without checking the type
Copy code
sealed class Sealed {
    data class SubSealed(val name: String, ...)
    data class SubSealed2(val name: String, ...)
}

val sealed: Seaded = Sealed.SubSealed

sealed.name // you can't, you need check the subtype

sealed class Sealed {
    abstract val name: String

    data class SubSealed(override val name: String, ...)
    data class SubSealed2(override val name: String, ...)
}

val sealed: Seaded = Sealed.SubSealed

sealed.name // you can, you havent to check the type
c
yeah but its abstract, because data classes are final so this could as well be an interface
h
A use case could be the same example but without Data classes and regular classes or objects instead and instead of abstract val name just val name. When the parent has derived properties, the use case for classes grows stronger, as interfaces cant do that (the same way) and implementing the derived properties in all subclasses is not dry and cant be used virtually :)
👍 2